【问题标题】:Is there a jQuery alternative to position: sticky?是否有 jQuery 替代位置:粘性?
【发布时间】:2017-02-14 16:43:53
【问题描述】:

假设我正在制作一个带有两个 col-lg-6 div 的 bootstrap 4 布局。左边的包含一个图像,右边的包含一堆文本,足够长以使页面可滚动。 由于 bootstrap 4 中的网格是基于 flex display 属性的,所以左边的 div 会自动垂直拉伸到右边的高度。 Bootstrap 4 有一个新的“sticky-top”类,它使用 position:sticky。 因此,如果将“sticky-top”类添加到左侧 div 内的图像并且页面向下滚动,则图像会随着页面滚动直到到达页面顶部,然后它会粘在顶部并保持粘性直到其父div的底部到达图像的底部,然后图像继续随着页面滚动。 不幸的是,所有现代浏览器仍然不完全支持 position:sticky ,所以我想知道是否有跨浏览器兼容的 jQuery 替代品? 我尝试通过 jquery 向图像添加一个额外的类,该类将位置更改为在 css 中固定,并在图像到达页面顶部时添加到图像中,然后我尝试在页脚进入视口后将其删除,但是使图像从视口中消失,而不是随页面一起滚动,因为在从其中删除附加类后,它会弹回其父 div 的顶部。

<header>header sticks to top</header>
<div>breadcrumbs that scroll along with the page go here</div>
<div class="row">
  <div class="col-12 col-lg-6">
    <img src="image.jpg" class="img-fluid" alt="image">
  </div>
  <div class="col-12 col-lg-6">
    <p>Bunch of random text long enough to make the page scrollable goes here</p>
  </div>
</div>
<footer>Footer stuff goes here</footer>

注意:图像只能在大型和超大型网格上粘贴,我宁愿不必使用任何插件。

【问题讨论】:

  • 是的,这完全可以用 javascript 来实现(即使你撒了一些 jquery。)在某些情况下,让它固定或不固定。
  • 你试过sticky-top吗?如果是这样,请发布代码,以便我了解您要粘贴的内容。
  • 到目前为止我尝试的一切都是废话,因此不值得发布。我需要在页面滚动后将图像粘贴到顶部,并在页脚到达视口后取消粘贴。基本上和他们在 apple.com 上的这个页面上的一样 apple.com/shop/buy-mac/macbook-pro?product=MLH42LL/…
  • 我写得很快,以防你能适应你的项目。 codepen.io/mcoker/pen/egxXjd
  • 它有效。我只需要稍微调整偏移量。谢谢。

标签: jquery css twitter-bootstrap


【解决方案1】:

我写得很快。将.sticky 类添加到您想要粘性的对象,并将.stickyTo 类添加到您想要粘性的父对象。它并不完美,但提供了一般概念,也许您可​​以针对您的项目进行调整。

var $sticky = $('.sticky'),
  $stickyTo = $('.stickyTo'),
  stickyToTop = $stickyTo.offset().top,
  stickyToBottom = stickyToTop + $stickyTo.outerHeight();

$(window).on('scroll', function() {
  var scroll = $(window).scrollTop(),
    stickyTop = $sticky.offset().top,
    stickyBottom = $sticky.offset().top + $sticky.outerHeight();
  
  if (stickyBottom >= stickyToBottom) {
    if (scroll < stickyTop) {
      $sticky.addClass('fixed').removeClass('abs');
    } else {
      $sticky.addClass('abs');
    }
  } else if (scroll > stickyToTop) {
    $sticky.addClass('fixed');
  } else if (scroll < stickyToTop) {
    $sticky.removeClass('abs').removeClass('fixed');
  }
});
.row {
  background: #ccc;
}

.fixed {
  position: fixed;
  top: 0;
  bottom: auto;
}

.abs {
  position: absolute;
  bottom: 0;
  top: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet">
<header>header sticks to top</header>
<div>breadcrumbs that scroll along with the page go here</div>
<div class="row stickyTo">
  <div class="col-12 col-lg-6">
    <img src="http://kenwheeler.github.io/slick/img/fonz1.png" class="img-fluid sticky" alt="image">
  </div>
  <div class="col-12 col-lg-6">
    <p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p><p>Bunch of random text long enough to make the page scrollable goes here</p>
  </div>
</div>
<footer>Footer stuff goes here</footer>

【讨论】:

  • 谢谢。我把它变成了一个函数,并在 $(document).ready 和 $(window).resize 上调用它,以防止浏览器使用响应式网格调整大小错误。
  • @GoranTesic 很好,很高兴它有帮助。
猜你喜欢
  • 2022-01-04
  • 1970-01-01
  • 2018-12-24
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
相关资源
最近更新 更多