今天分享的建站知识是利用JS实现DIV滚动至顶部后固定显示,具体代码如下:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>DIV滚动至顶部后固定</title> </head> <body style="height:2000px;"> <div style="height: 200px"></div> <div id="nav_keleyi_com" style="position:relative;top:0;background:#00f;width:100px; height:100px"> Test Div </div> <script type="text/javascript"> function menuFixed(id) { var obj = document.getElementById(id); var _getHeight = obj.offsetTop; window.onscroll = function () { changePos(id, _getHeight); } } function changePos(id, height) { var obj = document.getElementById(id); var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; if (scrollTop < height) { obj.style.position = 'relative'; } else { obj.style.position = 'fixed'; } } window.onload = function () { menuFixed('nav_keleyi_com'); } </script> </body>
扫描二维码手机查看该文章
文章引用:https://www.qinghuahulian.com/news/webzhishi/1389.html
- 上一篇:如何设置不带www的域名跳转到www域名上
- 下一篇:button按钮怎么实现超链接