开发者

Is there a more elegant way of calling a .ready with .events?

开发者 https://www.devze.com 2023-01-24 06:59 出处:网络
I just want to be as DRY as possible. $(docu开发者_运维百科ment).ready(function(){ var w= $(window).width();

I just want to be as DRY as possible.

$(docu开发者_运维百科ment).ready(function(){
    var w= $(window).width();
    w-= (w-6) % 266;
    if (w<272) w= 272;
    $('#grid_container').width(w);
});
jQuery.event.add(window, "load", resizeFrame);
jQuery.event.add(window, "resize", resizeFrame);
function resizeFrame() {
    var w= $(window).width();
    w-= (w-6) % 266;
    if (w<272) w= 272;
    $('#grid_container').width(w);
};


You can use the bind function:

$(window).bind('load resize', resizeFrame);

Notice that you can specify multiple events to bind by separating them with a space like shown above.

You can also check which event is triggered with type property of event object like this:

$(window).bind('load resize', function(e){
  alert(e.type);
});


I don't know if you want this:

$(window).bind("load",resizeFrame);

$(window).bind("resize",resizeFrame);

EDITED:

http://www.jsfiddle.net/t9fuH/

The more compressed option would be:

$(window).bind("load resize", resizeFrame);

function resizeFrame() {
    var w= $(window).width();
    w-=((w-6) % 266);
    w=w<272?272:w;    
    $('#grid_container').width(w);
};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号