js 實現頁面導航層級指示效果代碼

我們上網經常看到一些頁面層級指示的效果,比如淘寶、京東、百度百科等,用於指示用戶瀏覽頁面到哪裏了。可以起到很好的交互效果,現在我們就來自己去實現以下類似的效果。主要用到的API是scrollIntoView 和 getBoundingClientRect方法,原理是監聽頁面滾動元素,當元素距離到瀏覽器視口一定的位置的時候就證明元素開始出現在頁面的視口,就可以標識右邊的指示欄。

代碼如下:

<!DOCTYPE html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>H5 canvas繪製框選截圖和保存截圖代碼</title>
  <style>
    *{padding:0;margin:0;}
    #scrollBox{margin:0 auto;width:1000px;}
    #scrollBox .scroll-tips{position:fixed;right:10px;top:300px;width:100px;}
    #scrollBox .scroll-tips li {cursor:pointer;text-align:center;}
    #scrollBox .scroll-tips li.active {background:#ccc;}
    #scrollBox .ct {height: 550px;line-height:550px;text-align:center;font-size:40px;color:#fff;background:green;}
  </style>
</head>
<body>
  <div id="scrollBox">
    <div class="main">
      <div class="content">
        <h2 id="title1" class="title-bar" data-id="1">標題1</h2>
        <div class="ct">內容1</div>
      </div>
      <div class="content">
        <h2 id="title2" class="title-bar" data-id="2">標題2</h2>
        <div class="ct" style="height:847px;">內容2</div>
      </div>
      <div class="content">
        <h2 id="title3" class="title-bar" data-id="3">標題3</h2>
        <div class="ct" style="height:547px;">內容3</div>
      </div>
      <div class="content">
        <h2 id="title4" class="title-bar" data-id="4">標題4</h2>
        <div class="ct" style="height:427px;">內容4</div>
      </div>
      <div class="content">
        <h2 id="title5" class="title-bar" data-id="5">標題5</h2>
        <div class="ct" style="height:187px;">內容5</div>
      </div>
      <div class="content">
        <h2 id="title6" class="title-bar" data-id="6">標題6</h2>
        <div class="ct" style="height:232px;">內容6</div>
      </div>
    </div>
    <div class="scroll-tips">
      <ul>
        <li id="Li1" class="active" data-id="1">標題1</li>
        <li id="Li2" data-id="2">標題2</li>
        <li id="Li3" data-id="3">標題3</li>
        <li id="Li4" data-id="4">標題4</li>
        <li id="Li5" data-id="5">標題5</li>
        <li id="Li6" data-id="6">標題6</li>
      </ul>
    </div>
  </div>
  <script>
    let Li = $$('.scroll-tips li');
    let scrollTips = $$('.title-bar');
    function $$(str) {
      return document.querySelectorAll(str);
    };
    for (let i = 0, len = Li.length; i < len; i++) {
      // 點擊右側標識點左側滾動到窗口顯示位置
      Li[i].onclick = function () {
        let id = this.getAttribute('data-id');
        $$('.scroll-tips li.active')[0].classList.remove('active');
        this.classList.add('active');
        $$('#title' + id)[0].scrollIntoView();
      };
    };
    // 獲取當前頁面滾動到瀏覽器視口頂部位置的元素
    const getScrollTop = function (domArr) {
      for (let i = 0, len = domArr.length; i < len; i++) {
        let top = domArr[i].getBoundingClientRect().top;
        // 表示在一定範圍內允許的值
        if (top >= 0 && top <= 30) {
          return domArr[i];
        }
      }
    };
    // 監聽滾動方法
    window.onscroll = function () {
      let LiId = getScrollTop(scrollTips) && getScrollTop(scrollTips).getAttribute('data-id');
      if (LiId) {
        $$('.scroll-tips li.active')[0].classList.remove('active');
        $$('#Li' + LiId)[0].classList.add('active');
      }
    };
  </script>
</body>
</html>

運行結果如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章