我有一个带以下CSS的div.scroll_fixed:
.scroll_fixed {
position:absolute
top:210px
}
.scroll_fixed.fixed {
position:fixed;
top:0;
}
我正在使用以下jQuery代码,在div到达页面顶部时设置.fixed类。var top = $('.scroll_fixed').offset().top - parseFloat($('.scroll_fixed').css('margin-top').replace(/auto/, 0));
$(window).scroll(function (event) {
// what the y position of the scroll is
var y = $(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
$('.scroll_fixed').addClass('fixed');
} else {
// otherwise remove it
$('.scroll_fixed').removeClass('fixed');
}
});
在解决垂直滚动条固定的问题方面,这很有效。但是在浏览器窗口较小时,水平滚动会导致此固定div右侧内容的冲突。
我希望div可以随着内容水平滚动。
请问有谁能给我指点一下方向?我对JS / JQuery还不熟悉。
基本上,我希望它像这个示例中的第二个方框那样工作。