【发布时间】:2013-05-04 05:41:42
【问题描述】:
我一直很难将一个元素固定到页面左侧,但也能够上下滚动。
我已经尝试过将左侧位置更改为 $(window).scrollLeft() 的方法,但这会产生一个非常不稳定的动画。
我正在寻找的是这个小提琴的反面,但我不能让它工作:
var leftInit = $(".scroll_fixed").offset().left;
var top = $('.scroll_fixed').offset().top - parseFloat($('.scroll_fixed').css('margin- top').replace(/auto/, 0));
$(window).scroll(function(event) {
var x = 0 - $(this).scrollLeft();
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');
}
$(".scroll_fixed").offset({
left: x + leftInit
});
});
请注意,这个 div 是固定在顶部而不是左侧。我正在寻找相反的,固定的左侧而不是顶部。有什么想法吗?
我尝试编辑此小提琴,但无法正常工作。
【问题讨论】:
-
如果窗口水平滚动一半然后垂直滚动而不返回水平滚动到0,会发生什么?
标签: jquery css jquery-ui css-position