最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用原生JS實(shí)現(xiàn)愛(ài)奇藝首頁(yè)導(dǎo)航欄代碼實(shí)例

 更新時(shí)間:2019年09月19日 14:50:01   作者:袁藝明  
這篇文章主要介紹了用原生JS實(shí)現(xiàn)愛(ài)奇藝首頁(yè)導(dǎo)航欄代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

最近接到領(lǐng)導(dǎo)的任務(wù),要給外面的學(xué)生上幾節(jié)前端課,備課的時(shí)候準(zhǔn)備了一些小項(xiàng)目,在此記錄一下。

以下是愛(ài)奇藝首頁(yè)的一個(gè)導(dǎo)航欄,用原生js寫(xiě)的,靜態(tài)頁(yè)面效果如下:

代碼如下:

<html>
<head>
  <title>愛(ài)奇藝</title>
  <meta charset="utf-8">
  <style type="text/css">
    * {
      padding: 0;
      margin: 0;
    }
    .wrap {
      height: 520px;
      background-color: #000;
      width: 100%;
    }
    .wrap .img-wrap {
      height: 100%;
      margin: 0 auto;
      background-image: url('1.jpg');
      background-repeat: no-repeat;
      background-position: 50% 50%;
      background-size: auto 100%;
      position: relative;
    }
    /* 媒體查詢 */
    @media screen and (max-width: 2000px) {
      .wrap .img-wrap .item-list {
        right: 10%;
      }
    }
    @media screen and (max-width: 1600px) {
      .wrap .img-wrap .item-list {
        right: 8%;
      }
    }
    @media screen and (max-width: 1300px) {
      .wrap .img-wrap .item-list {
        right: 5%;
      }
    }
    .wrap .img-wrap .item-list {
      box-sizing: border-box;
      height: 100%;
      background-color: rgba(0,0,0,0.7);
      width: 280px;
      position: absolute;
      
      list-style: none;
      padding: 10px 0;
    }
    .wrap .img-wrap .item-list li {
      padding: 0 15px;
    }
    .wrap .img-wrap .item-list li.active {
      /*background-color: -webkit-linear-gradient(left, rgba(0,0,0,.3), rgba(0,0,0,0.1));*/
      background: linear-gradient(to right, rgba(0,0,0,.5), rgba(0,0,0,0));
      cursor: pointer;
    }
    .wrap .img-wrap .item-list li span {
      line-height: 40px;
      color: #eee;
      font-size: 16px;
    }
    .wrap .img-wrap .item-list li.active span {
      color: #00be06;
      display: block;
    }
    .wrap .img-wrap .item-list li.active span:nth-child(1) {
      font-size: 24px;
      transition: font-size 0.2s;
    }
    .wrap .img-wrap .item-list li.active span:nth-child(2) {
      display: none;
    }
  </style>
</head>
<body>
  <div class="wrap">
    <div class="img-wrap">
      <ul class="item-list">
      </ul>
    </div>
  </div>
  <script type="text/javascript">
    let items = [
        {
          title: '遇見(jiàn)幸福',
          desc: '24點(diǎn)體會(huì)人生百味',
          url: '1.jpg'
        },
        {
          title: '中國(guó)達(dá)人秀',
          desc: '真假岳岳在線劈叉',
          url: '2.jpg'
        },
        {
          title: '中國(guó)新說(shuō)唱',
          desc: '全國(guó)4強(qiáng)誕生!',
          url: '3.jpg'
        },
        {
          title: '做家務(wù)',
          desc: '魏大勛花錢做音樂(lè)',
          url: '4.jpg'
        },
        {
          title: '掃毒2',
          desc: '劉德華硬戰(zhàn)古天樂(lè)',
          url: '5.jpg'
        },
        {
          title: '加油',
          desc: '郝澤寧隔屏告白福子',
          url: '6.jpg'
        },
        {
          title: '小歡喜',
          desc: '宋倩喬衛(wèi)東重歸于好',
          url: '7.jpg'
        },
        {
          title: '謀愛(ài)上癮',
          desc: '契約夫婦遇感情危機(jī)',
          url: '8.jpg'
        },
        {
          title: '此食此客',
          desc: '啤酒花蛤夏日絕配',
          url: '9.jpg'
        },
        {
          title: '愛(ài)奇藝特別策劃',
          desc: '我們的70年',
          url: '10.jpg'
        }
    ];  // 需要展示的數(shù)據(jù),通常從后端獲取

    let curIndex = 0;  // 當(dāng)前索引
    let isAutoMove = true; // 是否可以自動(dòng)改變圖片    
    let interval = 1000; // 自動(dòng)輪播的間隔時(shí)間
    // 封裝函數(shù):生成新的標(biāo)簽
    function createTag(tag) {
      return document.createElement(tag);
    }
    // 封裝函數(shù):生成文本節(jié)點(diǎn)
    function createText(text) {
      return document.createTextNode(text);
    }
    // 封裝函數(shù):初始化列表
    function initItemList() {
      let ul = document.getElementsByClassName('item-list')[0];
      
      for (let i = 0; i < items.length; i++) {
        let li = createTag('li');
        if (i == 0) { li.classList.add('active') }
        let span1 = createTag('span');
        let span2 = createTag('span');
        let span3 = createTag('span');
        let text1 = createText(items[i].title);
        let text2 = createText(':');
        let text3 = createText(items[i].desc);
        span1.appendChild(text1);
        span2.appendChild(text2);
        span3.appendChild(text3);
        li.appendChild(span1);
        li.appendChild(span2);
        li.appendChild(span3);
        ul.appendChild(li);
        addHoverListener(li, i);
      } 
    }
    // 鼠標(biāo)hover右側(cè)欄時(shí)改變背景圖片及文字樣式
    function addHoverListener(trigger, index) {
      trigger.addEventListener('mouseenter', function () {
        curIndex = index;  // 保存當(dāng)前索引
        changeImage();
        activeText();
      });
    }
    // 根據(jù)index改變背景圖片
    function changeImage() {
      console.log('curIndex: ', curIndex);
      let imgUrl = items[curIndex].url;
      let imgWrap = document.getElementsByClassName('img-wrap')[0];
      imgWrap.style.cssText = "background-image: url('" + imgUrl + "')";
    }
    // 根據(jù)index改變右側(cè)激活文本的樣式
    function activeText() {
      let activeObj = document.getElementsByClassName('active')[0];
      let li = document.getElementsByTagName('li')[curIndex];
      if (activeObj) {
        activeObj.classList.remove('active');
      }
      li.classList.add('active');
    }
    // 鼠標(biāo)移入移出wrap區(qū)域時(shí)響應(yīng)關(guān)閉、開(kāi)啟自動(dòng)輪播
    function addEnterListener() {
      let wrap = document.getElementsByClassName('wrap')[0];
      wrap.addEventListener('mouseenter', function () {
        isAutoMove = false;
      });
      wrap.addEventListener('mouseleave', function () {
        isAutoMove = true;
      });
    }
    // 定時(shí)切換圖片:使用定時(shí)器setInterval(function(){}, time)
    function autoMove() {
      let timer = setInterval(function () {
        if (isAutoMove) {
          if (curIndex < 9) {
            curIndex ++;
          } else {
            curIndex = 0;
          }
          changeImage();
          activeText();
        }
      }, interval);
    }
    window.onload = function () {
      initItemList();
      addEnterListener();
      autoMove();
    }
  </script>
</body>
</html>

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

相關(guān)文章

最新評(píng)論

彰化市| 商城县| 凉城县| 通渭县| 大理市| 阜新市| 林州市| 万安县| 突泉县| 海林市| 蕉岭县| 乌鲁木齐县| 迁安市| 武邑县| 大石桥市| 衡山县| 历史| 中西区| 修文县| 威远县| 锡林郭勒盟| 林甸县| 华池县| 西林县| 河北区| 太谷县| 丰宁| 自治县| 忻城县| 绥滨县| 彭州市| 西丰县| 特克斯县| 南华县| 武清区| 定陶县| 乐业县| 礼泉县| 柞水县| 琼结县| 济源市|