js滾動到指定位置導航欄固定頂部


<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>js滾動到指定位置導航欄固定頂部</title>
    <style type="text/css">
       body{height: 2500px; margin: 0; padding: 0;}
      .banner{height: 250px; width: 100%; background: #e5e5e5;}
      .bignav{width: 100%; background: #000;}
      .nav{ background:#000; width: 1200px; margin: 0 auto; height: 45px;}
      .nav a{display: block; width: 200px;float: left; color: #fff; text-decoration: none; text-align: center; line-height: 45px;}
    </style>
  </head>
  <body>
    <div class="banner">
    </div>
    <div class="bignav" id="bignav">
      <div class="nav">
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>
      </div>
    </div>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <p>其他內容</p>
    <script type="text/javascript">
      window.onscroll=function(){
        var topScroll = get_scrollTop_of_body();//滾動的距離,距離頂部的距離
        var bignav = document.getElementById("bignav");//獲取到導航欄id
        if(topScroll > 250){ //當滾動距離大於250px時執行下面的東西
          bignav.style.position = 'fixed';
          bignav.style.top = '0';
          bignav.style.zIndex = '9999';
        }else{//當滾動距離小於250的時候執行下面的內容,也就是讓導航欄恢復原狀
          bignav.style.position = 'static';
        }
      }
      /*解決瀏覽器兼容問題*/
    function get_scrollTop_of_body(){ 
        var scrollTop; 
        if(typeof window.pageYOffset != 'undefined'){//pageYOffset指的是滾動條頂部到網頁頂部的距離 
            scrollTop = window.pageYOffset; 
        }else if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')        { 
            scrollTop = document.documentElement.scrollTop; 
        }else if(typeof document.body != 'undefined'){ 
            scrollTop = document.body.scrollTop; 
        } 
        return scrollTop; 
    } 

    </script>
  </body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM