滚动至顶部后固定
电脑及手机端多数使用滚动到顶部固定
操作方法
- 01
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>滚动至顶部后固定</title> <style type="text/css"> .wrapper{width:1000px;height:3000px;margin-left:auto;margin-right:auto;} .header{height:350px;} #nav_menu{padding:20px;position:relative;top:0;background:#125430;width:1000px;} a{display:inline-block;margin:0 10px;*display:inline;zoom:1;color:white;} </style> </head> <body> <div class="wrapper"> <div class="header"></div> <div id="nav_menu"> <a href="#" target="_blank">a</a> <a href="#" target="_blank">b</a> <a href="#" target="_blank">c</a> <a href="#" target="_blank">d</a> <a href="#" target="_blank">e</a> <a href="#" target="_blank">f</a> <a href="#" target="_blank">g</a> <a href="#" target="_blank">h</a></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'; } } </script> <script type="text/javascript"> window.onload = function(){ menuFixed('nav_menu'); } </script> </body> </html>