IE6浏览器下resize事件被执行了多次解决方法


Posted in Javascript onDecember 11, 2012

在IE浏览器下,一次resize事件被执行了多次。这是IE6和IE7的一个比较广为认知的问题,这个问题在这两个版本的浏览器中表现有所不同,通常IE6下会比IE7下更为糟糕。
现在已经有了一个较好的解决方案,让jquery.wresize.js这个jquery插件来帮你:
Js代码

/* 
=============================================================================== 
WResize is the jQuery plugin for fixing the IE window resize bug 
............................................................................... 
Copyright 2007 / Andrea Ercolino 
------------------------------------------------------------------------------- 
LICENSE: php">http://www.opensource.org/licenses/mit-license.php 
WEBSITE: http://noteslog.com/ 
=============================================================================== 
*/ 
( function( $ ) 
{ 
$.fn.wresize = function( f ) 
{ 
version = '1.1'; 
wresize = {fired: false, width: 0}; 
function resizeOnce() 
{ 
if ( $.browser.msie ) 
{ 
if ( ! wresize.fired ) 
{ 
wresize.fired = true; 
} 
else 
{ 
var version = parseInt( $.browser.version, 10 ); 
wresize.fired = false; 
if ( version < 7 ) 
{ 
return false; 
} 
else if ( version == 7 ) 
{ 
//a vertical resize is fired once, an horizontal resize twice 
var width = $( window ).width(); 
if ( width != wresize.width ) 
{ 
wresize.width = width; 
return false; 
} 
} 
} 
} 
return true; 
} 
function handleWResize( e ) 
{ 
if ( resizeOnce() ) 
{ 
return f.apply(this, [e]); 
} 
} 
this.each( function() 
{ 
if ( this == window ) 
{ 
$( this ).resize( handleWResize ); 
} 
else 
{ 
$( this ).resize( f ); 
} 
} ); 
return this; 
}; 
} ) ( jQuery );

如何使用,请看这个例子test page
Js代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" style="overflow:hidden;"> 
<head> 
<title> test window resize </title> 
<script type="text/javascript" src=http://www.hake.cc/kf/201109/"http://jquery.com/src/jquery-latest.pack.js"></script> 
<script type="text/javascript" src=http://www.hake.cc/kf/201109/"jquery.wresize.js"></script> 
<script type="text/javascript"> 
jQuery( function( $ ) 
{ 
function content_resize() 
{ 
var w = $( window ); 
var H = w.height(); 
var W = w.width(); 
$( '#content' ).css( {width: W-20, height: H-20} ); 
} 
$( window ).wresize( content_resize ); 
content_resize(); 
} ); 
</script> 
</head> 
<body> 
<div id="content" style="border: 1px dashed silver; position:absolute; overflow:auto;"> 
test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test 
</div> 
</body> 
</html>
Javascript 相关文章推荐
js表数据排序 sort table data
Feb 18 Javascript
prettify 代码高亮着色器google出品
Dec 28 Javascript
jQuery遍历json中多个map的方法
Feb 12 Javascript
JavaScript原生xmlHttp与jquery的ajax方法json数据格式实例
Dec 04 Javascript
原生js编写autoComplete插件
Apr 13 Javascript
JavaScript学习小结之被嫌弃的eval函数和with语句实例详解
Aug 01 Javascript
AngularJS教程之MVC体系结构详解
Aug 16 Javascript
COM组件中调用JavaScript函数详解及实例
Feb 23 Javascript
jQuery NProgress.js加载进度插件的简单使用方法
Jan 31 jQuery
Vue实现数据表格合并列rowspan效果
Nov 30 Javascript
js实现3D照片墙效果
Oct 28 Javascript
js操作两个json数组合并、去重,以及删除某一项元素
Sep 22 Javascript
什么是json和jsonp,jQuery json实例详详细说明
Dec 11 #Javascript
JavaScript子窗口ModalDialog中操作父窗口对像
Dec 11 #Javascript
javascript中window.event事件用法详解
Dec 11 #Javascript
js 延迟加载 改变JS的位置加快网页加载速度
Dec 11 #Javascript
js控制CSS样式属性语法对照表
Dec 11 #Javascript
javascript向flash swf文件传递参数值注意细节
Dec 11 #Javascript
ajax的hide隐藏问题解决方法
Dec 11 #Javascript
You might like
PHP 应用程序的安全 -- 不能违反的四条安全规则
2006/11/26 PHP
thinkPHP使用pclzip打包备份mysql数据库的方法
2016/04/30 PHP
php查看一个变量的占用内存的实例代码
2020/03/29 PHP
Draggable Elements 元素拖拽功能实现代码
2011/03/30 Javascript
js showModalDialog参数的使用详解
2014/01/07 Javascript
jquery解析JSON数据示例代码
2014/03/17 Javascript
JS+CSS实现Li列表隔行换色效果的方法
2015/02/16 Javascript
jQuery简单获取键盘事件的方法
2016/01/22 Javascript
AngularJS  自定义指令详解及实例代码
2016/09/14 Javascript
微信小程序 教程之模块化
2016/10/17 Javascript
学习JavaScript图片预加载模块
2016/11/07 Javascript
使用nodejs下载风景壁纸
2017/02/05 NodeJs
详解使用vue-router进行页面切换时滚动条位置与滚动监听事件
2017/03/08 Javascript
js经验分享 JavaScript反调试技巧
2018/03/10 Javascript
深入了解响应式React Native Echarts组件
2019/05/29 Javascript
jquery操作checkbox的常用方法总结【附测试源码下载】
2019/06/10 jQuery
微信小程序 wx.getUserInfo引导用户授权问题实例分析
2020/03/09 Javascript
vue proxy 的优势与使用场景实现
2020/06/15 Javascript
vue-calendar-component 封装多日期选择组件的实例代码
2020/12/04 Vue.js
[33:39]DOTA2上海特级锦标赛C组小组赛#2 LGD VS Newbee第二局
2016/02/27 DOTA
Python 数据结构之旋转链表
2017/02/25 Python
python Matplotlib画图之调整字体大小的示例
2017/11/20 Python
Python设计模式之MVC模式简单示例
2018/01/10 Python
python实现文件助手中查看微信撤回消息
2019/04/29 Python
django admin.py 外键,反向查询的实例
2019/07/26 Python
Python简易计算器制作方法代码详解
2019/10/31 Python
Django {{ MEDIA_URL }}无法显示图片的解决方式
2020/04/07 Python
Python使用configparser读取ini配置文件
2020/05/25 Python
python利用os模块编写文件复制功能——copy()函数用法
2020/07/13 Python
英国性能汽车零件和发动机配件在线:Maxpeedingrods
2019/11/05 全球购物
求∏的近似值,直到最后一项的绝对值小于指定的数
2016/02/12 面试题
.NET程序员的几道面试题
2012/06/01 面试题
群众路线查摆问题整改措施
2014/10/10 职场文书
债务授权委托书范本
2014/10/17 职场文书
2019职场单身人才调研报告:互联网行业单身比例最高
2019/08/07 职场文书
Java9新特性之Module模块化编程示例演绎
2022/03/16 Java/Android