javascript - 如何使用javascript检测不可滚动元素中的滚动事件和方向?

标签 javascript scroll overflow

我正在尝试模拟终端滚动行为,它只是立即将 View 移动 3 行,而没有平滑的滚动动画。

这是我简化的 CSS 和 HTML 结构:

body {
  overflow: hidden;
  font-size: 13px;
  line-height: 1.2em;
}
section {
  width: 100%;
}
section#tabs {
  position: fixed;
  top: 0;
  background-color: grey;
}
section#main {
  margin: 15px 0;
}
section#controls {
  position: fixed;
  bottom: 0;
  background-color: grey;
}
section#imgView {
  position: fixed;
  top: 100%;
  background-color: red;
}
<html>
  <body>
    <article>
      <div data-reactroot>
        <section id="tabs">
          <span>[abc]</span>
          <span>[bcd]</span>
          <span>[cde]</span>
          <span>[def]</span>
          <span>[efg]</span>
        </section>
        <section id="main">
          <div>some texts that is long enough to make this snippet properly represent some shape I want to show</div>
          <div>some texts that is long enough to make this snippet properly represent some shape I want to show</div>
          <div>some texts that is long enough to make this snippet properly represent some shape I want to show</div>
          <div>some texts that is long enough to make this snippet properly represent some shape I want to show</div>
          <div>some texts that is long enough to make this snippet properly represent some shape I want to show</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>some texts that is long enough to make this snippet properly represent some shape I want to show</div>
          <div>some texts that is long enough to make this snippet properly represent some shape I want to show</div>
          <div>some texts that is long enough to make this snippet properly represent some shape I want to show</div>
          <div>some texts that is long enough to make this snippet properly represent some shape I want to show</div>
          <div>some texts that is long enough to make this snippet properly represent some shape I want to show</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
          <div>another texts to tell the difference on the height</div>
        </section>
        <section id="controls">
          <div>This will always be at the bottom.</div>
        </section>
        <section id="imgView">
          <div>You're not supposed to see this sentence.</div>
        </section>
      </div>
    </article>
  </body>
</html>

选项卡控件部分将粘在浏览器中各自的边缘,并且imgView将不可见,除非某些代码调用通过更改其位置相关属性来实现。

我这样做是为了让正文有 overflow: hide;,我无法使用将当前滚动位置与前一个滚动位置进行比较的方法。

最佳答案

只需监听滚动事件,然后向上或向下滚动 3 行。

var lineHeight = 18;
var scrollStep = lineHeight * 3;
var lastScrollY = 0;
var scrollContainer = document.querySelector("#main");

scrollContainer.addEventListener("scroll", function () {
    if (scrollContainer.scrollTop > lastScrollY) {
        scrollContainer.scrollTop = lastScrollY + scrollStep;
    } else if (scrollContainer.scrollTop < lastScrollY) {
        scrollContainer.scrollTop = lastScrollY - scrollStep;  
    }

    lastScrollY = scrollContainer.scrollTop;
});

关于javascript - 如何使用javascript检测不可滚动元素中的滚动事件和方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37019995/

相关文章:

Javascript 用字符串替换元素

javascript - innerHtml 不适用于 ionic 2 管道,如何操作?

javascript - React Ant 设计步骤 -- NO 'Check Icon'

iphone - UIWebView 不会滚动到末尾/底部

html - 将带有两个 div 标签的页面分成两部分,每个部分都有自己的滚动条

html - 水平滚动条不显示

javascript - 我可以编辑现有 Ionic 组件的 html 吗?

android-studio - 在 Android 模拟器中禁用鼠标滚轮集成

java - 使用java在Selenium WebDriver中垂直向下滚动和向上滚动

html - 如何使用html和css使div与另一个div重叠