向下滑动时,导航栏固定
代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } img{ display: block; margin: 0 auto; } img#search.dhgd{ position: fixed; left: 50%; margin-left: -596px; top: 0px; } </style> <script src="https://www.jb51.net/jquery.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> $(function(){ // 给浏览器加滚动条事件 $(window).scroll(function(){ //获得当前已滚动上去的距离 var t = $(document).scrollTop(); console.log('=================='+t) if(t>126){ $("#search").addClass('dhgd'); }else{ $("#search").removeClass('dhgd'); } }) }) </script> </head> <body> ![](imgs/img01.png) ![](imgs/img02.png) ![](imgs/img03.png) </body> </html>