老师,麻烦帮看看我的代码为什么不能实现: 二级菜单的展开啊? 谢谢哦

来源:6-1 案例总结

qq_默默_77

2018-07-27 02:12:14

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>模范课堂范例</title>

    <style type="text/css">

    *{margin: 0px; padding: 0px}

    body{position: relative;}


    #background-box{

        width: 1200px; height: 460px;

        margin-left: 50%;

        left: -600px;

        position: relative;

        z-index: 1;

    }


    #background-box .italic-arrow{ position: absolute; color:white; z-index: 4;}

     #background-box .left-fix-shade{              <!-- 左固定遮挡块-->

        position: relative;

        height: 460px; width: 246px;

        background-color: black;  opacity: 0.4;

        position: absolute;

        top: 0px;

        z-index: 2; 

        

    }

    

    #background-box #listBox{

           list-style: none;

           position: absolute;

           top:3px;

           background-color:transparent;

           text-align: left;

           left: 24.5px;

           z-index: 4;

    }




     #background-box #listBox .list{

        width: 197px;  height: 69px;

        border-bottom: 0.6px solid lightgray;

        line-height: 69px;

        color: white;

     }

#background-box  #listBox .list a{  text-decoration:none; color:white; display: block; float: left;}

#background-box  #listBox .list i{ display: block; float: right;}



#background-box #shade{ width: 246px; height: 69px; background-color: black; opacity: 0.4; z-index: 3; position: absolute; top: 0px }



#arrow{  width: 954px; height: 76px; position: absolute; margin-left: 50%; left:-354px;top: 272px; z-index: 5}                

#arrow #arrow1{ width: 40px; height: 76px;  float: right;  position: relative;}

#arrow #arrow1 img{ display: block; position: absolute; right: 0px; margin-top: 50%; }

#arrow #arrow2{ width: 40px; height: 76px; float: left; position: relative; }

#arrow #arrow2 img{display: block; position: absolute; left: 0px; margin-top: 50% }


#circles{ width: 99px; height: 15px; position: absolute; z-index: 5; margin-left: 50%; left:500px; top:410px;}

#circles .outerCircle{ width: 15px; height:15px; background-color: white; float: left; margin-right: 18px; position: relative; border-radius:7.5px;}

#circles .innerCircle{ width: 13px; height:13px; background-color: gray; margin-left: 50%; left: -6.5px; margin-top:50%; top:-6.5px;position: absolute; border-radius:6.5px;}


.sub-page{

    width: 730px; height: 460px; 

    z-index: 5; 

    position: absolute;

    margin-left: 50%; left:-354px;

    top: 0px;

    background-image: url("img/fe.png");

    

   

    

  }

.sub-page .content{ margin-top:25px; margin-left: 40px; }

.sub-page .title{ color: red; font-weight:bold }

.sub-page .content .sub-row{ margin-top: 28px; margin-bottom: 28px; }

.sub-page .content .sub-row>span{ font-weight: bold }

.sub-page .content .sub-row span{ margin-right: 15px}

.sub-page .content .sub-row a{ text-decoration: none; color: gray; margin-right: 15px; }

      

    </style>


</head>


<body>

    <div id="background-box">

        <img src="img/bg2.jpg" width="1200px" height="460px" id="backImg">


        <div class="left-fix-shade"></div>    <!-- 左固定遮挡块-->


        <ul id="listBox">   <!-- 列表-->

          <li class="list1 list"><a href="#">手机、配件</a><i>&gt</i></li>        <!--<a>标签和<i>标签 都变成块状元素各左浮和右浮-->

          <li class="list2 list"><a href="#">电脑</a><i>&gt</i></li>

          <li class="list3 list"><a href="#">家用电器</a><i>&gt</i></li>

          <li class="list4 list"><a href="#">家具</a><i>&gt</i></li>

       </ul>


       <div id="shade"></div>    <!-- 动态遮挡块-->

    </div>


       <!-- 导航栏箭头-->

    <div id="arrow">

        <div id="arrow1"><img src="img/arrow2.png"></div>

        <div id="arrow2"><img src="img/arrow1.png"></div>

    </div>

 

<!-- 导航栏小圆点-->

<div id="circles">

    <div  id="outerCircle1" class="outerCircle"><div class="innerCircle"></div></div>

    <div  id="outerCircle2" class="outerCircle"><div class="innerCircle"></div></div>

    <div  id="outerCircle3" class="outerCircle"><div class="innerCircle"></div></div>

</div>

    

<!-- 导航鼠标悬停后出现的分页 1-->

<div class="sub-page" id="subPage1">

  <div class="content">

    <div class="title">手机、配件</div>

      <div class="sub-row">

      <span>手机通讯:</span>

      <a href="#">手机</a>

      <span>/</span>

      <a href="#">手机维修</a>

      <span>/</span>

      <a href="#">以旧换新</a>

       </div>


        <div class="sub-row">

      <span>手机配件:</span>

      <a href="#">手机壳</a>

      <span>/</span>

      <a href="#">手机存储卡</a>

      <span>/</span>

      <a href="#">数据线</a>

      <span>/</span>

      <a href="#">充电器</a>

      <span>/</span>

      <a href="#">电池</a>

       </div>


        <div class="sub-row">

       <span>运营商:</span>

       <a href="#">中国联通</a>

       <span>/</span>

       <a href="#">中国移动</a>

       <span>/</span>

       <a href="#">中国电信</a>

        </div>


        <div class="sub-row">

        <span>智能设备:</span>

        <a href="#">智能手环</a>

        <span>/</span>

        <a href="#">智能家居</a>

        <span>/</span>

        <a href="#">智能手表</a>

        <span>/</span>

        <a href="#">其他配件</a>

        </div>


        <div class="sub-row">

          <span>娱乐: </span>

          <a href="#">耳机</a>

          <span>/</span>

          <a href="#">音响</a>

          <span>/</span>

          <a href="#">收音机</a>

          <span>/</span>

          <a href="#">麦克风</a>

        </div>

</div>


<!-- 导航鼠标悬停后出现的分页 2-->

<div class="sub-page" id="subPage2">

  <div class="content">

    <div class="title">电脑</div>

      <div class="sub-row">

      <span>电脑:</span>

      <a href="#">笔记本</a>

      <span>/</span>

      <a href="#">平板</a>

      <span>/</span>

      <a href="#">一体机</a>

       </div>


        <div class="sub-row">

      <span>电脑配件:</span>

      <a href="#">显示器</a>

      <span>/</span>

      <a href="#">CPU</a>

      <span>/</span>

      <a href="#">主板</a>

      <span>/</span>

      <a href="#">硬盘</a>

      <span>/</span>

      <a href="#">电源</a>

      <span>/</span>

      <a href="#">显卡</a>

       <span>/</span>

      <a href="#">其它配件</a>

       </div>


        <div class="sub-row">

       <span>游戏设备:</span>

       <a href="#">游戏机</a>

       <span>/</span>

       <a href="#">耳机</a>

       <span>/</span>

       <a href="#">游戏软件</a>

        </div>


        <div class="sub-row">

        <span>网络产品:</span>

        <a href="#">路由器</a>

        <span>/</span>

        <a href="#">网络机顶盒</a>

        <span>/</span>

        <a href="#">交换机</a>

        <span>/</span>

        <a href="#">存储卡</a>

        <span>/</span>

        <a href="#">网卡</a>

        </div>


        <div class="sub-row">

          <span>外部产品:</span>

          <a href="#">鼠标</a>

          <span>/</span>

          <a href="#">键盘</a>

          <span>/</span>

          <a href="#">U盘</a>

          <span>/</span>

          <a href="#">移动硬盘</a>

          <span>/</span>

          <a href="#">鼠标垫</a>

          <span>/</span>

          <a href="#">电脑清洁</a>

        </div>

</div>


<!-- 导航鼠标悬停后出现的分页 3-->

<div class="sub-page" id="subPage3">

  <div class="content">

    <div class="title">家用电器</div>

      <div class="sub-row">

      <span>电视:</span>

      <a href="#">国产品牌</a>

      <span>/</span>

      <a href="#">韩国品牌</a>

      <span>/</span>

      <a href="#">欧美品牌</a>

       </div>


        <div class="sub-row">

      <span>空调:</span>

      <a href="#">柜式</a>

      <span>/</span>

      <a href="#">中央</a>

      <span>/</span>

      <a href="#">壁挂式</a>

       </div>


        <div class="sub-row">

       <span>冰箱:</span>

       <a href="#">多门</a>

       <span>/</span>

       <a href="#">对开门</a>

       <span>/</span>

       <a href="#">三门</a>

       <span>/</span>

       <a href="#">双门</a>

        </div>


        <div class="sub-row">

        <span>洗衣机:</span>

        <a href="#">滚筒式洗衣机</a>

        <span>/</span>

        <a href="#">迷你洗衣机</a>

        <span>/</span>

        <a href="#">洗烘一体机</a>

        </div>


        <div class="sub-row">

          <span>厨房电器:</span>

          <a href="#">油烟机</a>

          <span>/</span>

          <a href="#">洗碗机</a>

          <span>/</span>

          <a href="#">燃气灶</a>

        </div>

</div>


<!-- 导航鼠标悬停后出现的分页 4-->

<div class="sub-page" id="subPage4">

  <div class="content">

    <div class="title">家具</div>

      <div class="sub-row">

      <span>家纺:</span>

      <a href="#">被子</a>

      <span>/</span>

      <a href="#">四件套</a>

      <span>/</span>

      <a href="#">床垫</a>

       </div>


        <div class="sub-row">

      <span>灯具:</span>

      <a href="#">台灯</a>

      <span>/</span>

      <a href="#">顶灯</a>

      <span>/</span>

      <a href="#">节能灯</a>

      <span>/</span>

      <a href="#">应急灯</a>

       </div>


        <div class="sub-row">

       <span>厨具:</span>

       <a href="#">烹饪锅具</a>

       <span>/</span>

       <a href="#">餐具</a>

       <span>/</span>

       <a href="#">菜板刀具</a>

        </div>


        <div class="sub-row">

        <span>家装:</span>

        <a href="#">地毯</a>

        <span>/</span>

        <a href="#">沙发垫套</a>

        <span>/</span>

        <a href="#">装饰字画</a>

        <span>/</span>

        <a href="#">照片墙</a>

         <span>/</span>

        <a href="#">窗帘</a>

        </div>


        <div class="sub-row">

          <span>生活日用:</span>

          <a href="#">收纳用品</a>

          <span>/</span>

          <a href="#">浴室用品</a>

          <span>/</span>

          <a href="#">雨伞雨衣</a>

        </div>

</div>



<!-- JS部分-->

    <script type="text/javascript">

        var listBox=document.getElementById("listBox");

        var list=document.getElementById("listBox").getElementsByTagName("li");               //获取 list的对象集合

        var backgroundBox=document.getElementById("background-box");     //获取背景盒子对象

        var backImg=document.getElementById("backImg");

        var shade=document.getElementById("shade");      //获取shade对象         

        var arrow1=document.getElementById("arrow1");    //获取导航arrow对象   

        var arrow2=document.getElementById("arrow2");



        var subPage1=document.getElementById("subPage1");        //获取导航栏Subpage1对象

        var subPage2=document.getElementById("subPage2");        //获取导航栏Subpage2对象

        var subPage3=document.getElementById("subPage3");        //获取导航栏Subpage3对象

        var subPage4=document.getElementById("subPage4");        //获取导航栏Subpage4对象


        var outerCircle1=document.getElementById("outerCircle1");

              


        var outerCircle2=document.getElementById("outerCircle2");

        var outerCircle3=document.getElementById("outerCircle3");

        




        arrow1.onmouseover=function(){ arrow1.style.backgroundColor="black"; arrow1.style.opacity="0.4" }

        arrow2.onmouseover=function(){ arrow2.style.backgroundColor="black"; arrow2.style.opacity="0.4" }

        arrow1.onmouseout=function(){  arrow1.style.backgroundColor=""}

        arrow2.onmouseout=function(){  arrow2.style.backgroundColor=""}

        

        list[0].onmouseover=function(){  shade.style.top="3px";    subPage1.style.display="block" }

        list[0].onmouseout=function(){  subPage1.style.display="none" }



        list[1].onmouseover=function(){  shade.style.top="72.6px";    subPage2.style.display="block" }

        list[1].onmouseout=function(){  subPage2.style.display="none" }


        list[2].onmouseover=function(){  shade.style.top="142.2px";    subPage3.style.display="block"  }

        list[2].onmouseout=function(){  subPage3.style.display="none" }


        list[3].onmouseover=function(){  shade.style.top="211.8px";    subPage4.style.display="block"  }

        list[3].onmouseout=function(){  subPage4.style.display="none" }

        


       


      //实现连播功能:

        var arr=["img/bg1.jpg","img/bg2.jpg","img/bg3.jpg"];  


        var i=0;

       function swiftPic()

       {   if(i==2){ backImg.src=arr[i]; i=0;  }else{ backImg.src=arr[i]; i=i+1; }

              

       }

setInterval(swiftPic,500);


outerCircle1.onclick=function(){ backImg.src="img/bg1.jpg" }

outerCircle2.onclick=function(){ backImg.src="img/bg2.jpg" }

outerCircle3.onclick=function(){ backImg.src="img/bg3.jpg" }



          


     




    </script>

    

</body>

</html>


写回答

1回答

好帮手慕夭夭

2018-07-27

根据上传的代码测试 , js中当鼠标从第一个菜单移出后 ,设置整体的下拉菜单隐藏 :

http://img.mukewang.com/climg/5b5a89840001d1b011750204.jpg

如下鼠标移入第二个时 ,整体的div是隐藏的 ,

http://img.mukewang.com/climg/5b5a89be0001570910320730.jpg

建议同学调整结构 , 把二级菜单写在以及菜单里面 ,如下结构 :

http://img.mukewang.com/climg/5b5a8cce0001cd7b04420250.jpg

整体的盒子sidebar_item可以设置相对定位 , 然后二级菜单盒子设置绝对定位达到位置要求 , 然后设置隐藏 , 当鼠标移入到整体的盒子sidebar_item时显示二级菜单哦

实战一下吧  , 祝学习愉快 ! 望采纳

1

0 学习 · 36712 问题

查看课程