0

我有这个 jQuery 代码:

  $(window).resize(function() {         
if ($(window).width() <= 1250) {
  $('#topbar').css('position', 'static');
} else {
  $('#topbar').css('position', 'fixed');
}
});

问题是,当您放大小于 1250 然后重新加载页面时,这个 jQuery 不起作用。那么我该如何解决这个问题?

4

2 回答 2

3

尝试触发调整大小处理程序可能:

$(window).resize(function () {
    if ($(this).width() <= 1250) {
        $('#topbar').css('position', 'static');
    } else {
        $('#topbar').css('position', 'fixed');
    }
}).triggerHandler('resize');
于 2013-05-13T13:03:40.533 回答
2

在 CSS 中执行此操作:

#topbar {
    position: static
}

@media screen and (min-width: 1250px) {
    #topbar {
        position: fixed
    }
}

现场演示: http: //jsfiddle.net/krrvA/show/(我使用背景颜色来表示切换。水平调整窗口大小,颜色将在 1250px 处改变。)

于 2013-05-13T13:08:15.243 回答