开发者

Bind Ready Function and Resize Event

开发者 https://www.devze.com 2023-03-28 05:01 出处:网络
In jQuery, is it poss开发者_如何学JAVAible to somehow \"bind\" an action to both the ready function and a resize event? I\'m trying to add/remove classes on resize and I want the initial class state t

In jQuery, is it poss开发者_如何学JAVAible to somehow "bind" an action to both the ready function and a resize event? I'm trying to add/remove classes on resize and I want the initial class state to be there on ready.


The easiest way to do this is to trigger the resize event immediately after binding it:

$(document).ready(function() {
    $(window).resize(function() {
        // code to run on resize
    }).resize(); // trigger resize handlers
});


function onresize()
{
  // do your stuff here
}

// called on "init"
onresize();

// called when window is resized
$(window).resize(function(){ onresize(); });
0

精彩评论

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