Visibility for summary of HTML interview questions, and visibility for question Summary

Source: Internet
Author: User

Visibility for summary of HTML interview questions, and visibility for question Summary
1. Page visibility)

It mainly provides two attributes, one event (both on the document Object ):
1. attributes:
1.1. hidden: gets or sets the visibility of the current page, a boolean value;
1.2. visibilityState: gets the visibility status of the current page. The value is "hidden" and "visibility ".
2. Event: visibilityChange: The event triggered when the page visibility changes

3. Because the support conditions of various browsers are inconsistent, you must add the prefix of private attributes, such as webkit, moz, ms, and o. Not supported in ie9.

3. Application scenarios of visibility:
3.1. Video Playback Switching
3.2. User status verification

Ii. Sample Code: 2.1. Utils class, provides page visibility detection capabilities, mainly to achieve compatibility with various browsers:
Const BROWER_PREFIX = ['webkit', 'moz', 'ms', 'O', '']; class Utils {constructor () {this. isPageVisibilitySupport = false; this. prefix = '';}/*** get the attribute name * @ param prefix {string} prefix * @ param value {string} attribute name */prefixToCamel (prefix, value) {if (prefix) {return prefix + value. slice (0, 1 ). toUpperCase () + value. slice (1);} return value;} calculatePageVisibilitySupport () {var that = this; BROWER_PR EFIX. forEach (function (data) {if (! That. isPageVisibilitySupport & document [that. prefixToCamel (data, 'did')]! = Undefined) {that. isPageVisibilitySupport = true; that. prefix = data ;}}); return that. isPageVisibilitySupport;} isHidden () {if (this. calculatePageVisibilitySupport () {return document [this. prefixToCamel (this. prefix, 'den den ')];} return undefined;} visibilityState () {if (this. calculatePageVisibilitySupport () {return document [this. prefixToCamel (this. prefix, "visibilitystate")];} return undefined ;}} export default Utils;
2.2. core class, providing externally accessible static methods and attributes:
import utils from 'src/utils';var _utils = new utils();class Core {    static visibilityChange(callback){        if( _utils.calculatePageVisibilitySupport() && typeof callback == 'function'){            return document.addEventListener(Core.prefix + 'visibilitychange',function(event){                this.hidden = Core.hidden;                this.visibilityState = Core.visibilityState;                callback.call(this,event);            });            return undefined;        }    }}Core.hidden = _utils.isHidden();Core.visibilityState = _utils.visibilityState();export default Core;

VisibilityChange method: triggers event binding when the page visibility value is changed.

Iii. Source Code GIT address

This package uses the karma framework + jasmine for unit testing. Source code through babel

Git@code.csdn.net: cqhaibin/visibilityproject. git

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.