• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

louisremi/jquery-smartresize: Debounced and Throttled Resize Events for jQuery

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

louisremi/jquery-smartresize

开源软件地址:

https://github.com/louisremi/jquery-smartresize

开源编程语言:

JavaScript 100.0%

开源软件介绍:

Debounced and Throttled Resize Events for jQuery

It has always been a pain to deal with cross browser issues of the window's resize event:

  • in IE, many resize events fire as long as the user continues resizing the window,
  • Chrome an Safari behave like IE, but resize events always fire two by two,
  • Firefox used to fire one resize event at the end of the resizing, but now behaves like IE,
  • Opera behaves like IE, but fires resize events at a reduced rate.

This project offers two scripts, each providing a special jQuery event that make resize more manageable:

  • jquery.debouncedresize.js: adds a special event that fires once after the window has been resized,
  • jquery.throttledresize.js: adds a special event that fires at a reduced rate (no more double events from Chrome and Safari).

The Demo should help you make your choice.

Note to previous users: jquery.debouncedresize.js is the equivalent of the old jquery.smartresize.js, only the name of the special event changes. Update is not required unless you want to add jquery.throttledresize.js to a page page that already has jquery.smartresize.js.

Binding / Unbinding

Simply bind your special event just like a normal resize event.

$(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" );

Threshold

Both special events have a .threshold option:

  • in jquery.debouncedresize.js, it defines the interval used to determine if two resize events are part of the same debouncedresize event. Defaults to 150 (milliseconds)
  • in jquery.throttledresize.js, it defines the number of animation ticks (or frames) between each throttledresize event. Defaults to 0 (tick), which means that it's going to fire at a maximum of 60fps.

They can be modified globally once the script has been loaded:

// increase the threshold to 250ms
$.event.special.debouncedresize.threshold = 250;

// decrease the firing rate to a maximum of 30fps
$.event.special.throttledresize.threshold = 1;
// 2 <=> 20fps, 3 <=> 15fps, ...

(Synchronous) Trigger

Triggering those events is achieved using jQuery's standard API:

$(window).trigger( "debouncedresize" );

It's also possible to execute the handler of any listener synchronously (without the delays):

$(window).trigger( "throttledresize", [true] );

Minimalist Standalone Version

Most of the time, I find myself using debouncedresize just to register a single listener on window. As it turns out, all the features I need actually fit in 91 bytes:

// debulked onresize handler
function on_resize(c,t){onresize=function(){clearTimeout(t);t=setTimeout(c,100)};return c};

Using it is pretty simple:

on_resize(function() {
  // handle the resize event here
  ...
});

Initializing a page (by executing the resize handler when the page loads) couldn't be easier:

on_resize(function() {
  ...
})(); // these parenthesis does the trick

No files are provided for this function, simply copy/paste it from this README.

License

MIT licensed http://louisremi.mit-license.org/

Copyright (c) 2012 Louis-Rémi Babé.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap