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