Fixed ie8 & chrome window resize event multiple executions _ javascript tips-js tutorial

Source: Internet
Author: User
Window. onresize event-specific event binder v0.1, used to solve the lteie8 & amp; chrome and other bugs that may occur when the native window. resize event is executed multiple times. The Code is as follows:


/**
* Window. onresize event-specific event binder v0.1 Alucelx
* Http://www.cnblogs.com/Alucelx/archive/2011/10/20/2219263.html
*
* This vulnerability is used to solve the multiple execution bugs of lte ie8 & chrome and other native window. resize events that may occur.
*
*
* Add: add an event handle.
* Remove: Delete the event handle.
*
*/
Var onWindowResize = function (){
// Event queue
Var queue = [],
IndexOf = Array. prototype. indexOf | function (){
Var I = 0, length = this. length;
For (; I <length; I ++ ){
If (this [I] === arguments [0]) {
Return I;
}
}
Return-1;
};
Var isResizing ={}, // mark the visible area size status, used to eliminate the bug of multiple executions of the window. onresize event in lte ie8/chrome
Lazy = true, // lazy execution mark
Listener = function (e) {// event listener
Var h = window. innerHeight | (document.doc umentElement & document.doc umentElement. clientHeight) | document. body. clientHeight,
W = window. innerWidth | (document.doc umentElement & document.doc umentElement. clientWidth) | document. body. clientWidth;
If (h === isResizing. h & w === isResizing. w ){
Return;
} Else {
E = e | window. event;
Var I = 0, len = queue. length;
For (; I <len; I ++ ){
Queue [I]. call (this, e );
}
IsResizing. h = h,
IsResizing. w = w;
}
}
Return {
Add: function (fn ){
If (typeof fn = 'function '){
If (lazy) {// lazy execution
If (window. addEventListener ){
Window. addEventListener ('resize', listener, false );
} Else {
Window. attachEvent ('onresize', listener );
}
Lazy = false;
}
Queue. push (fn );
} Else {}
Return this;
},
Remove: function (fn ){
If (typeof fn = 'undefined '){
Queue = [];
} Else if (typeof fn === 'function '){
Var I = indexOf. call (queue, fn );
If (I>-1 ){
Queue. splice (I, 1 );
}
}
Return this;
}
};
}. Call (this );


Use this object to bind the resize event of the window.
Example:

The Code is as follows:


Var _ fn = function () {document. body. innerHTML + = "1 "};
OnWindowResize. add (_ fn)
. Add (function () {document. body. innerHTML + = "2 "})
. Add (function () {document. body. innerHTML + = "3 "})
. Remove (_ fn );

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.