简体   繁体   中英

Changing CSS with jQuery on browser resize

I've got the following code: it changes the position of some navigation elements, depending on the browser width.

It does this by using $(window).width() , and then making the change in pixels (not percentages). This works great. However, when the user changes the browser window, the calculations are off.

Is there a way for me to refire the css changes on browser resize -- or just reload the div? Reloading the page on resize works okay, but it's only a small div that needs fixing.

if (window.location.href.indexOf('archive') >= 0) {
    archive.css({left: nw / 2 - aw / 2});
    films.css({right: - nw / 2 - fw / 2});
        $('.index').css({left: nw / 2 - iw / 2 });

} else if (window.location.href.indexOf('films') >= 0) {
    archive.css({left: - nw / 2 - aw / 2});
    films.css({right:  nw / 2 - fw / 2});
        $('a.index').css({left: - nw / 2 + iw / 2 });
} else if (window.location.href.indexOf('index') >= 0) {


}
$(window).resize(function() {
    // do something on resize
});

should do the trick.

By the way, CSS media queries could be a better choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM