欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)導(dǎo)航欄樓層特效

 更新時(shí)間:2020年01月01日 13:03:56   作者:KaiSarH  
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)導(dǎo)航欄樓層特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JS實(shí)現(xiàn)導(dǎo)航欄樓層的具體代碼,供大家參考,具體內(nèi)容如下

知識(shí)點(diǎn)

1.多個(gè)事件有沖突的時(shí)候,需要設(shè)置flag判斷是什么事件,進(jìn)而進(jìn)行后續(xù)操作。
2.樓層效果就是判斷scrollTop和offsetTop的關(guān)系
3.引入工具庫(kù)工具庫(kù)

運(yùn)行效果

導(dǎo)航與界面實(shí)現(xiàn)互動(dòng)

代碼

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    *{margin: 0;padding: 0;list-style: none;border:0;}
    html, body, ul{width: 100%;height: 100%;}
    #ul li{
      width: 100%;
      height: 100%;
      text-align: center;
      font-size: 30px;
      /*background-color: red;*/
      color: #fff;
    }

    #ol{
      width: 80px;
      background-color: #ccc;
      position: fixed;
      left: 50px;
      top: 200px;
      border: 1px solid #fff;
    }

    #ol li{
      text-align: center;
      line-height: 30px;
      border-bottom: 1px solid #fff;
      color: #fff;
      cursor: pointer;
    }

    #ol li.current{
      background-color: orangered;
    }
  </style>
</head>
<body>
  <!--導(dǎo)航-->
  <ol id="ol">
    <li class="current">第1層</li>
    <li>第2層</li>
    <li>第3層</li>
    <li>第4層</li>
    <li>第5層</li>
  </ol>
  <!--樓層-->
  <ul id="ul">
    <li>第1層內(nèi)容</li>
    <li>第2層內(nèi)容</li>
    <li>第3層內(nèi)容</li>
    <li>第4層內(nèi)容</li>
    <li>第5層內(nèi)容</li>
  </ul>

<script src="../../00MyTools/MyTools.js"></script>
<script>
  window.addEventListener('load', function (ev) {
     // 1. 獲取標(biāo)簽
    var ol = myTool.$('ol'), ul = myTool.$('ul');
    var ulLis = ul.children;
    var olLis = ol.children;

    // 是否是點(diǎn)擊產(chǎn)生的滾動(dòng)
    var isClick = false;

    // 2. 上色
    var colorArr = ['red', 'green', 'blue', 'purple', 'yellow'];
    for (var i = 0; i < colorArr.length; i++) {
      ulLis[i].style.backgroundColor = colorArr[i];
    }

    // 3. 監(jiān)聽(tīng)導(dǎo)航點(diǎn)擊
    for (var j = 0; j < olLis.length; j++) {
      var olLi = olLis[j];
      (function (index) {
        olLi.addEventListener('click', function () {
          isClick = true;
          for (var i = 0; i < olLis.length; i++) {
            olLis[i].className = '';
          }
          this.className = 'current';
          // document.documentElement.scrollTop = index * myTool.client().height;

          myTool.slowMoving(document.documentElement, {'scrollTop': index * myTool.client().height}, function () {
            isClick = false;
          });
        });
      })(j)
    }

    // 4. 監(jiān)聽(tīng)滾動(dòng)
    var roll = 0;
    window.addEventListener('scroll', function (ev1) {
      if(!isClick){
        // 4.1 獲取頭部滾動(dòng)偏移的高度
        roll = Math.ceil(Number(myTool.scroll().top));

        // 4.2 遍歷
        for (var i = 0; i < ulLis.length; i++) {
          // 4.3 判斷
          if(roll >= ulLis[i].offsetTop){
            for (var j = 0; j < olLis.length; j++) {
              olLis[j].className = '';
            }
            olLis[i].className = 'current';
          }
        }
      }
    })
  });
</script>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論