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 相关文章推荐
html中table数据排序的js代码
Aug 09 Javascript
qTip2 精致的基于jQuery提示信息插件
Feb 17 Javascript
JS与C#编码解码
Dec 03 Javascript
100个不能错过的实用JS自定义函数
Mar 05 Javascript
Javascript获取当前时间函数和时间操作小结
Oct 01 Javascript
20个实用的JavaScript技巧分享
Nov 28 Javascript
JS获取当前脚本文件的绝对路径
Mar 02 Javascript
微信小程序简单的canvas裁剪图片功能详解
Jul 12 Javascript
微信小程序通过js实现瀑布流布局详解
Aug 28 Javascript
js实现简单的日历显示效果函数示例
Nov 25 Javascript
高性能js数组去重(12种方法,史上最全)
Dec 21 Javascript
详解JavaScript中的数据类型,以及检测数据类型的方法
Sep 17 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版(3)
2006/10/09 PHP
探讨:如何通过stats命令分析Memcached的内部状态
2013/06/14 PHP
在yii中新增一个用户验证的方法详解
2013/06/20 PHP
用PHP和Shell写Hadoop的MapReduce程序
2014/04/15 PHP
php实现paypal 授权登录
2015/05/28 PHP
PHP中关于php.ini参数优化详解
2020/02/28 PHP
php使用event扩展的io复用测试的示例
2020/10/20 PHP
javascript获取元素CSS样式代码示例
2013/11/28 Javascript
Jquery 返回json数据在IE浏览器中提示下载的问题
2014/05/18 Javascript
jquery easyui使用心得
2014/07/07 Javascript
JS+CSS实现弹出全屏灰黑色透明遮罩效果的方法
2014/12/20 Javascript
Windows系统中安装nodejs图文教程
2015/02/28 NodeJs
javascript中的正则表达式使用指南
2015/03/01 Javascript
JavaScript操作DOM元素的childNodes和children区别
2015/04/01 Javascript
浅谈Javascript中Object与Function对象
2015/09/26 Javascript
浅析webpack 如何优雅的使用tree-shaking(摇树优化)
2017/08/16 Javascript
JavaScript实现各种排序的代码详解
2017/08/28 Javascript
微信小程序switch开关选择器使用详解
2018/01/31 Javascript
JS pushlet XMLAdapter适配器用法案例解析
2020/10/16 Javascript
nuxt 页面路由配置,主页轮播组件开发操作
2020/11/05 Javascript
Python压缩和解压缩zip文件
2015/02/14 Python
使用Python写个小监控
2016/01/27 Python
Python程序中的观察者模式结构编写示例
2016/05/27 Python
在PyCharm中控制台输出日志分层级分颜色显示的方法
2019/07/11 Python
python中68个内置函数的总结与介绍
2020/02/24 Python
英国领先的亚洲旅游专家:Wendy Wu Tours
2018/01/21 全球购物
英国邮购活的植物主要供应商:Gardening Direct
2019/01/28 全球购物
英国时尚泳装品牌:Maru Swimwear
2019/10/06 全球购物
3D空间设计学生找工作的自我评价
2013/10/28 职场文书
教育学专业实习生的自我鉴定
2013/11/26 职场文书
打架检讨书800字
2014/01/10 职场文书
爷爷追悼会答谢词
2014/01/24 职场文书
学校安全生产承诺书
2014/05/23 职场文书
银行贷款委托书范本
2014/10/11 职场文书
现实表现材料范文
2014/12/23 职场文书
2015大学生入党个人自传
2015/06/26 职场文书