[英]Position fixed on scroll starts before it reaches the top (top div)
现在,我已经使#filter
(我的侧边栏白色选择列表)在到达顶部时变得fixed
了,并在滚动过程中一直停留在该位置,直到到达底部并释放为止。
我的问题是我的头部有一个<div>
框,该框定位为fixed
,因此从获取到页面顶部的#filter
不会被固定。 这意味着在将它固定到固定位置之前,我会先将其抢走。 如何在到达顶部之前将其fixed
为40px?
$(function() {
var top = $('#filter').offset().top,
footTop = $('#outside_footer_wrapper').offset().top,
maxY = footTop - $('#filter').outerHeight();
console.log(top , footTop, maxY);
$(window).scroll(function(evt) {
var y = $(this).scrollTop();
console.log(y);
if (y > top) {
console.log('mayor');
$('#filter').addClass('fixed').removeAttr('style');
if (y > maxY-130){
var min = y - maxY + 130;
console.log('mayor y menor', min);
$('#filter').css('top','-'+min+'px');
}
} else {
$('#filter').removeClass('fixed');
}
});
});
添加/更改if语句至此:
console.log(y);
if (y >= (top-y)) {
这是链接jsfiddle
更新
将其更改回此$('#filter').css('top','-'+min+'px');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.