Fix the Resize event for ie8&chrome under Windows multiple execution _javascript tips

Source: Internet
Author: User
Tags event listener
Copy Code code as follows:

/**
* Window.onresize Event-specific event-binding device v0.1 Alucelx
* http://www.cnblogs.com/Alucelx/archive/2011/10/20/2219263.html
* <description>
* Used to solve multiple bugs of LTE IE8 & Chrome and other possible native window.resize events.
* </description>
* <methods>
* Add: Add event handle
* Remove: Delete event handle
* </methods>
*/
var onwindowresize = function () {
Event queues
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 visual Area dimension status to eliminate multiple bugs for window.onresize events in LTE Ie8/chrome
Lazy = true,//lazy Execute tag
Listener = function (e) {//Event listener
var h = window.innerheight | | (document.documentelement && document.documentElement.clientHeight) | | Document.body.clientHeight,
w = window.innerwidth | | (document.documentelement && document.documentElement.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);

Bind window's Resize event, use this object
Example:
Copy Code code 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.