我需要得到window.pageYOffset || document.documentElement.scrollTop;值。确定1)如果用户滚过标题2)和方向st<三角洲&& lastScrollTop!== 0
$ …
您正在使用dispay:flex on body元素。这不是一个好主意,而是使用div。只需在身体上弯曲(其他元素都很好),你就可以了!
<德尔> 显示:flex; 德尔> <德尔> 弯曲方向:柱; 德尔>
我个人认为改变显示器的显示类型是一个糟糕的选择 body 元件。它是根容器,谁知道每个浏览器如何指定它的显示。恕我直言,最好创建自己的根元素并从那里开始。
body
CodePen示例
var $main = $('main'); $main.on('scroll', () => { var pos = $main.scrollTop(); var isTop = pos == 0; $main.toggleClass('top', isTop); var isBottom = pos + $main.innerHeight() >= $main.prop('scrollHeight'); $main.toggleClass('bottom', isBottom); console.log('is triggering', pos); });
html, body { height: 100vh; margin: 0; padding: 0; } #root { height: 100vh; display: flex; flex-direction: column; } .sticky-header, .sticky-footer { flex: 0 0 auto; background: purple; } .sticky-content { flex: 1 1 auto; overflow-y: scroll; background-color: green; } .top { background-color: yellow; } .bottom { background-color: orange; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="root"> <header class="sticky-header"> <h1>I'm Sticky</h1> </header> <main class="sticky-content top"> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> <p>This is a large amount of scrollable content.</p> </main> <footer class="sticky-footer"> <small>I'm sticky</small> </footer> </div>