5行js代码搞定导航吸顶效果
一、HTML布局 首先写HTML布局 <body> <div id="wrap"></div> </body> 二、CSS样式 给点简单的样式 <style> *{ margin: 0; padding: 0; } body{ height: 2000px; background-image: linear-gradient(-180deg, #15f09d 0%, #25A0FF 50%, #fca72b 100%); } #wrap{ background-color: rgba(0,0,0,0.2); width: 100%; height: 100px; margin-top: 100px; } #wrap[data-fixed="fixed"]{ position: fixed; top:0; left: 0; margin: 0; } </style> 三、JS代码 1、面向过程 直接编写5行代码搞定 <script> var obj = document.getElementById("wrap"); var ot = obj.offsetTop; document.onscroll = function () { var st = document.body.scrollTop || document.documentElement.scrollTop; obj