简体   繁体   English

在浏览器调整大小时使用jQuery更改CSS

[英]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). 它通过使用$(window).width() ,然后以像素(而非百分比)为单位进行更改。 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? 有没有办法让我在浏览器调整大小时重新启动CSS更改-或只是重新加载div? Reloading the page on resize works okay, but it's only a small div that needs fixing. 可以通过调整大小的方式重新加载页面,但是只有一个小div需要修复。

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. 顺便说一下,CSS媒体查询可能是一个更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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