【发布时间】:2016-10-25 06:27:50
【问题描述】:
当我滚动过去最初的“elementpos”时,我的粘性侧边栏工作,它进入一个固定位置并跟随屏幕,我想要它做的就是贴在它旁边的 div 底部旁边一次我滚动到某个点。我尝试使用页脚高度 + 固定数量的像素,以便在滚动到该点后使其进入绝对位置。出于某种原因,一旦我滚动到我希望它去的点,它就不起作用了。
JS FIDDLE:https://jsfiddle.net/j05t35ax/2/
这是我的 jquery 脚本。
$(document).scroll(function() {
var scrollpos = $(window).scrollTop();
var elementpos = $('.textbody-aa').offset().top;
var boxesoffsetbottom = $('.boxes-buttons').offset().top + (494);
var footerheight = $('.footer').offset().top + (-25);
if (scrollpos >= elementpos) {
$(".boxes-buttons").addClass("fixed")
$(".boxes-buttons").removeClass("static")
}
else if (boxesoffsetbottom >= footerheight) {
$(".boxes-buttons").addClass("staticbottom")
$(".boxes-buttons").removeClass("fixed")
$(".boxes-buttons").removeClass("static")
}
else {
$(".boxes-buttons").removeClass("staticbottom")
$(".boxes-buttons").removeClass("fixed")
$(".boxes-buttons").addClass("static")
}
});
.fixed {
position: fixed;
right: 0px;
top: 0px;
}
.static {
position: static;
}
.staticbottom {
position: absolute;
bottom: 145px;
}
【问题讨论】:
-
这将是一个很棒的 jsfiddle,如果你可以格式化一个那么它会更容易调试?
-
jsfiddle.net/j05t35ax/1 这正是我的意思,.staticbottom 是当红色 div 超过红色 div 时我希望它去的地方。我希望底部平整
-
抱歉小提琴是一个好的开始,但缺少页脚,可能还有其他红色 div?
-
jsfiddle.net/j05t35ax/2 哎呀,没保存。我们去
标签: jquery scroll sidebar sticky