js 滚动到一定位置导航定位在页面最顶部
2017-11-30 16:38
190 查看
js 滚动到一定位置导航定位在页面最顶部
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js 滚动导航定位</title> <style> body { margin: 0; text-align: center; font-family: sans-serif; } .fixed { position: fixed; top: 0; } .sticky { width: 100%; background: #F6D565; padding: 25px 0; text-transform: uppercase; } </style> </head> <body> <p style="margin-bottom:100px;">Scroll this page.</p> <div class="sticky"><h3>Super amazing header</h3></div> <p style="margin-top:500px;">Still there?</p> <p style="margin-top:500px;">Yep!</p> <p style="margin-top:500px;">Scroll so hard!</p> <script> var sticky = document.querySelector('.sticky'); var origOffsetY = sticky.offsetTop; function onScroll(e) { window.scrollY >= origOffsetY ? sticky.classList.add('fixed') : sticky.classList.remove('fixed'); } document.addEventListener('scroll', onScroll); </script> </body> </html>
posted @ 2017-11-30 16:38 <_/> 阅读(...) 评论(...) 编辑 收藏
相关文章推荐
- JS 侧栏导航点击定位页面内容位置及滚动条滚动改变导航元素对应状态
- html js点击按钮滚动跳转定位到页面指定位置(DIV)的方法代码
- 返回顶部,js,css,页面离开顶部一定距离后出现返回顶部,点击后滚动回顶部,支持IE,FF,chrome ,safari,opera[摘自布布分享,tech.bubufx.com]
- js实现html页面滚动条向下拉到一定的程度时,某个div就固定在顶部,向上拉时返回原位置。
- JQ:当页面滚动到一定位置之后,让元素固定在顶部,小于位置后恢复原来的位置
- js在页面滚动到一定位置时触发事件?
- html页面滚动到一定位置显示回到顶部按钮
- 【转】js在页面滚动到一定位置时触发事件?
- JS-JQ实现页面滚动时元素智能定位(顶部-其他部位)
- js实现滚动条滚动到某个位置便自动定位某个tr
- js,html 监听页面滚动高度 点击返回顶部
- 点击导航,滑动到网页中的指定位置(JS实现滑动锚点定位)
- js实现页面滚动切换导航栏/点击导航栏跳转到指定位置
- JS实现页面进入、返回定位到具体位置
- div盒子position:fixed定位后无法滚动到页面顶部了
- js页面滚动时层智能浮动定位实现
- js,jquery滚动/跳转页面到指定位置的实现思路
- js页面滚动时层智能浮动定位实现(jQuery)
- 滚动条事件,当页面滚动到距顶部一定高度时某DIV自动隐藏和显示
- 页面滚动定位导航添加样式