热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

跨浏览器resize事件分析

resize事件原生事件分析window一次resize事件:IE7触发3次,IE8触发2次,IE9触发1次,IE10触发1次Chrome触发1次FF触发2次Opera触发1次Safari触发1次场景分析windowresize时,部分组件需要重置大小(一次);部分组件不需要重置大小;开源库分析

resize事件

原生事件分析

window一次resize事件:

  • IE7 触发3次, IE8 触发2次, IE9 触发1次, IE10 触发1次

  • Chrome 触发1次

  • FF 触发2次
  • Opera 触发1次
  • Safari 触发1次

场景分析
  • window resize时,部分组件需要重置大小(一次);部分组件不需要重置大小;

开源库分析

优点:使用简便

  $('#div1').on('resize', function (e) {
    console.log('[div1] resize');
  })
  $('#div1').resize(function (e) {
    console.log('[div1] resize2');
  });

缺点:内部使用轮询,性能堪忧

function loopy() {
    // Start the polling loop, asynchronously.
    timeout_id = window[ str_setTimeout ](function(){
      // Iterate over all elements to which the 'resize' event is bound.
      elems.each(function(){
        var elem = $(this),
          width = elem.width(),
          height = elem.height(),
          data = $.data( this, str_data );

        // If element size has changed since the last time, update the element
        // data store and trigger the 'resize' event.
        if ( width !== data.w || height !== data.h ) {
          elem.trigger( str_resize, [ data.w = width, data.h = height ] );
        }
      });
      // Loop.
      loopy();
    }, jq_resize[ str_delay ] );
};

优点:分Debounced和Throttled两种类型,类型明确

缺点:使用不算简易

$(window).on("debouncedresize", function( event ) {
    // Your event handler code goes here.
});

// or...
$(window).on("throttledresize", function( event ) {
    // Your event handler code goes here.
});

// unbind at will
$(window).off( "debouncedresize" );

结论

大多数场景使用jquery-smartresize的Debounced即可满足一次调用即可


推荐阅读
author-avatar
企鹅
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有