下拉菜單

代碼實(shí)現(xiàn)

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
      *{
          margin: 0;
          padding: 0;
          box-sizing: border-box;
      }
      html,body{
          width: 100%;
          height:100%;
      }
      body{font-size:62.5%}
      .nav{
          width: 100%;height: 50px;
          background-color: antiquewhite;
          display: flex;
          justify-content: center;
      }
      .page-header{
          width: 1200px;
          height: 100%;
          /* background-color: aqua; */
          display: flex;
          justify-content:space-evenly;
          list-style: none;
      }
      .page-header > .item{
          padding: 10px 50px;
          font-size: 18px;
          text-align: center;
          cursor: pointer;
          position: relative;
      }
      .page-header > .item:hover{
          background-color: #d0a554;
          color: #333;
          font-weight: 500;
      }
      .childern{
          display: none;
          position: absolute;
          top: 50px;
          left: 0;
          list-style: none;
          padding: 10px 20px;
          background-color: bisque;
      }
      .childern > li{
          line-height: 50px;
      }
      .childern > li:hover{
          background-color: #f2f2f1;
          color: #333;
      }
  </style>
</head>
<body>
  <div class="nav">
      <ul class="page-header">
          <li class="item">首頁</li>
          <li class="item">文章
               <ul class="childern">
                  <li>發(fā)表文章</li>
                  <li>我的文章</li>
                  <li>關(guān)注的文章</li>
                  <li>喜歡的文章</li>
              </ul>
          </li>
          <li class="item">相冊
              <ul class="childern">
                  <li>我的相冊</li>
                  <li>上傳照片</li>
              </ul>
          </li>
          <li class="item">消息
              <ul class="childern">
                  <li>我的私信</li>
                  <li>我的留言</li>
              </ul>
          </li>
      </ul>
  </div>
  <script>
      let _lis = document.getElementsByClassName("item");
      for(let i = 0;i < _lis.length;i++){
          _lis[i].onmouseenter = function(){
              let _childern = this.children[0];
              _childern.style.display = "block";
          }
          _lis[i].onmouseenter = function(){
              let _childern = _lis[i].getElementsByClassName("childern")
              if(_childern.length > 0){
                  let c = _childern[0];
                       c.style.display = "block";
              }
              
          }
          _lis[i].onmouseleave = function(){
              let _childern = _lis[i].getElementsByClassName("childern")
              if(_childern.length > 0){
                  let c = _childern[0];
                       c.style.display = "none";
              }
               
          }
      }
  </script>
</body>
</html>

網(wǎng)頁效果


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容