【问题标题】:Jquery Sticky side bar won't stick to bottom of websiteJquery Sticky 侧边栏不会粘在网站底部
【发布时间】: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


【解决方案1】:

试试这个,你的if条件只需要考虑顶部和底部,所以我将窗口高度添加到滚动位置并添加蓝色div的高度来计算它的底部位置,让红色div恢复到其固定状态。并确保在每种情况下都解决了所有三个类。

这是一个没有类交换的不同小提琴:https://jsfiddle.net/manoeuvres/p7o265nr/ 它提供了不同的优点/缺点。

$(document).scroll(function() {
    var scrollpos = $(window).scrollTop();
    var winHeight = $(window).height();
    var elementpos = $('.textbody-aa').offset().top;
    var elementHeight = $('.textbody-aa').height();
    var boxesoffsetbottom = $('.boxes-buttons').offset().top + (340);
    var footerheight = $('.footer').offset().top - (50);

    if (scrollpos >= elementpos && (scrollpos+winHeight) < (elementHeight+50)) {
        $(".boxes-buttons").addClass("fixed");
        $(".boxes-buttons").removeClass("static");
        $(".boxes-buttons").removeClass("staticbottom");
    } 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");
    }

});
.footer{
  display:block;
  bottom:0px;
  width: 100%;
  height: 200px;
  background-color: pink;
}

.textbody-aa{
  margin-bottom: 50px;
  margin-top: 50px;
  height: 1000px;
  width:150px;
  background-color: #123d80;
}

.boxes-buttons{
  height: 340px;
  width:150px;
  background-color: red;
  position: absolute;
  right:50px;
  top: 50px;
}

.fixed{
  position: fixed;
}

.static{
  position: absolute;
  right:50px;
  top: 50px;
}

.staticbottom{
  position: absolute;
  right:50px;
  top: 710px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class ="textbody-aa">
Please stop at the bottom and the top
</div>
<div class ="boxes-buttons">
no
</div>

<footer class="footer">come here</footer>

【讨论】:

  • 由于某种原因,当我尝试在我的项目中使用类交换一个时,我仍然遇到同样的问题,它在小提琴中有效,但在我的网站上当它打算交换到“.staticbottom " 它改为交换为 ".static"。我还将它用于多个页面,具有不同的“.textbody-aa”高度大小,在这种情况下使用 Jquery 版本会更好吗?
  • 听起来在这种情况下非交换版本值得测试。它的好处是适应不同的 div 大小。类交换的好处是更容易为表格带来新的和不同的属性,例如更改颜色和添加边框等 - 在非交换版本中这些都是尽可能的,只是不太容易添加。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多