Copy codeThe Code is as follows:
/**
* Window. onresize event-specific event binder v0.1 Alucelx
* Http://www.cnblogs.com/Alucelx/archive/2011/10/20/2219263.html
* <Description>
* This vulnerability is used to solve the multiple execution bugs of lte ie8 & chrome and other native window. resize events that may occur.
* </Description>
* <Methods>
* Add: add an event handle.
* Remove: Delete the event handle.
* </Methods>
*/
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:
Copy codeThe 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 );