【问题标题】:Fix an element until everything inside is scrolled修复一个元素,直到里面的所有东西都被滚动
【发布时间】:2017-10-12 09:03:39
【问题描述】:

我之前问过这个问题,但我无法很好地表述它,但这就是我想要实现的。滚动单页网站时,所有部分的高度均为 100%。当我到达一个有内容要向下滚动的部分时,我想禁用滚动整个网页,直到该部分的内容完全向下滚动。此小提琴中的示例:

div {
  height: 100vh;
}

.blue {
  background: blue;
}

.red {
  background: red;
}

.white {
  background: white;
}

.black {
  background: black;
}

div.content-wrapper {
  overflow: auto;
  width: 40%;
}
<div class="red">

</div>
<div class="blue">

</div>
<div class=white>
  <div class="content-wrapper">
    <div class="content">
      Test TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest
      TestTest TestTest TestTest TestTest TestTest Test
    </div>
    <div class="content">
      Test TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest
      TestTest TestTest TestTest TestTest TestTest Test
    </div>
    <div class="content">
      Test TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest TestTest
      TestTest TestTest TestTest TestTest TestTest Test
    </div>
  </div>
</div>
<div class="black">

</div>

当我们到达白色部分时,它应该被修复,我们应该监听滚动事件并以某种方式翻译或滚动里面的元素(测试测试 divs),当我们走到最后 - 允许滚动到黑色部分。

有人遇到过类似的问题吗?我不知道该怎么做,因此我无法提供我尝试过的代码。我在 AngularJS 工作。

【问题讨论】:

  • 不是让空白宽度更简单 100% 吗??
  • @Zoedia 是,但在实际项目中却不是,它位于中间位置

标签: jquery html css scroll


【解决方案1】:

您找到解决方案了吗?如果没有,试试这个。

$('.white').on('mousewheel DOMMouseScroll', function(e) {
  var e0 = e.originalEvent,
    delta = e0.wheelDelta || -e0.detail;

  var scrolled = $(".content-wrapper").scrollTop() + (delta < 0 ? 1 : -1) * 20;
  if (!($(".content-wrapper").scrollTop() == 0 && scrolled < 0) && //check if hit top and still scrolling above
      !($(".content-wrapper")[0].scrollHeight - $(".content-wrapper").scrollTop() <= $(".content-wrapper").outerHeight() &&
        scrolled + $(".content-wrapper").outerHeight() > $(".content-wrapper")[0].scrollHeight) //check if scrollbar hit the .white scroll bottom and still scrolling below
     ) {
    $(".content-wrapper").scrollTop(scrolled);
    e.preventDefault();
  }
});

【讨论】:

    【解决方案2】:

    你可以试试:

    $( '.scrollable' ).on( 'mousewheel DOMMouseScroll', function ( e ) {
        var e0 = e.originalEvent,
            delta = e0.wheelDelta || -e0.detail;
    
        this.scrollTop += ( delta < 0 ? 1 : -1 ) * 40;
        e.preventDefault();
    });
    

    演示:https://jsbin.com/howojuq/edit?js,output

    或参考:How to prevent page scrolling when scrolling a DIV element?

    【讨论】:

    • hm,我不确定我明白发生了什么,Demo 不起作用,它会抛出错误的网关错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    相关资源
    最近更新 更多