2012-11-02 77 views
1

我無法獲得此代碼的工作 - 我想要的是,當我將鼠標懸停在圓上時,菜單會出現,如果我將鼠標從圓或菜單上移開,它就會消失。我嘗試做的任何事情都是非常奇怪的,比如onmouseover在圓上,然後onmouseout在菜單上,菜單似乎在我離開圓圈後消失,我嘗試過使用除Div以外的其他構造,但仍然沒有運氣,我決定發帖在這裏看看有沒有人可以幫忙。請看以下代碼:菜單我無法上班

JS:

<script> 

function circleAnimOn() { 
    //show menu 
    document.getElementById("cssmenuDiv").style.display="block"; 
    document.getElementById("cssmenuDiv").style.visibility="visible"; 
} 

function circleAnimOff() { 
    //hide menu 
    document.getElementById("cssmenuDiv").style.display="none"; 
    document.getElementById("cssmenuDiv").style.visibility="hidden"; 
} 
</script> 

HTML:

<body> 

<div class="divCircle" id="divCircle" onMouseOver="circleAnimOn();"> 
</div> 

<div class="cssmenuDiv" id="cssmenuDiv"><!-- cssmenuDiv --> 

<div id='cssmenu'> 
<ul> 
    <li class='active '><a href='index.html'><span>Home</span></a></li> 
    <li class='has-sub '><a href='#'><span>Products</span></a> 
     <ul> 
     <li class='has-sub '><a href='#'><span>Product 1</span></a> 
      <ul> 
       <li><a href='#'><span>Sub Item</span></a></li> 
       <li><a href='#'><span>Sub Item</span></a></li> 
      </ul> 
     </li> 
     <li class='has-sub '><a href='#'><span>Product 2</span></a> 
      <ul> 
       <li><a href='#'><span>Sub Item</span></a></li> 
       <li><a href='#'><span>Sub Item</span></a></li> 
       <li><a href='#'><span>Sub Item</span></a></li> 
      </ul> 
     </li> 
     </ul> 
    </li> 
    <li><a href='#'><span>About</span></a></li> 
    <li><a href='#'><span>Contact</span></a></li> 
</ul> 
</div> 

</div><!-- cssmenuDiv --> 

CSS:

body { 

    text-align:center; 
} 

#divCircle 
{ 
    width:30px; 
    height:30px; 
    background-color:#CCC; 
    margin-left:auto; 
    margin-right:auto; 
border:2px solid; 
border-radius:25px; 
-moz-border-radius:25px; /* Firefox 3.6 and earlier */ 
} 

#divCircle:hover { 
    background-color:#093; 
} 

#circle { width: 50px; height: 50px; background: red; -moz-border-radius: 50px; -webkit-border-radius: 50px; border-radius: 50px;} 

.cssmenuDiv { 
    display:none; 
    visibility:hidden; 
    margin-left:750px; 

} 

/* Some stylesheet reset */ 
#cssmenu > ul{ 
    list-style: none; 
    margin: 0; 
    padding: 0; 
    vertical-align: baseline; 
    line-height: 1; 
} 

/* The container */ 
#cssmenu > ul { 
    display: block; 
    position: relative; 
    width: 150px; 
} 

    /* The list elements which contain the links */ 
    #cssmenu > ul li { 
     display: block; 
     position: relative; 
     margin: 0; 
     padding: 0; 
     width: 150px; 
    } 

     /* General link styling */ 
     #cssmenu > ul li a { 
      /* Layout */ 
      display: block; 
      position: relative; 
      margin: 0; 
      border-top: 1px dotted #fff; 
      border-bottom: 1px dotted #d9d9d9; 
      padding: 11px 20px; 
      width: 110px; 

      /* Typography */ 
      font-family: Helvetica, Arial, sans-serif; 
      color: #3dafea; 
      text-decoration: none; 
      text-transform: uppercase; 
      text-shadow: 0 1px 0 #fff; 
      font-size: 13px; 
      font-weight: 300; 

      /* Background & effects */ 
      background: #eaeaea; 
     } 

     /* Rounded corners for the first link of the menu/submenus */ 
     #cssmenu > ul li:first-child>a { 
      border-top-left-radius: 4px; 
      border-top-right-radius: 4px; 
      border-top: 0; 
     } 

     /* Rounded corners for the last link of the menu/submenus */ 
     #cssmenu > ul li:last-child>a { 
      border-bottom-left-radius: 4px; 
      border-bottom-right-radius: 4px; 
      border-bottom: 0; 
     } 


     /* The hover state of the menu/submenu links */ 
     #cssmenu > ul li>a:hover, #cssmenu > ul li:hover>a { 
      color: #fff; 
      text-shadow: 0 1px 0 rgba(0, 0, 0, .3); 
      background: #54cdf1; 
      background: -webkit-linear-gradient(bottom, #54cdf1, #74d7f3); 
      background: -ms-linear-gradient(bottom, #54cdf1, #74d7f3); 
      background: -moz-linear-gradient(bottom, #54cdf1, #74d7f3); 
      background: -o-linear-gradient(bottom, #54cdf1, #74d7f3); 
      border-color: transparent; 
     } 

     /* The arrow indicating a submenu */ 
     #cssmenu > ul .has-sub>a::after { 
      content: ''; 
      position: absolute; 
      top: 16px; 
      right: 10px; 
      width: 0px; 
      height: 0px; 

      /* Creating the arrow using borders */ 
      border: 4px solid transparent; 
      border-left: 4px solid #3dafea; 
     } 

     /* The same arrow, but with a darker color, to create the shadow effect */ 
     #cssmenu > ul .has-sub>a::before { 
      content: ''; 
      position: absolute; 
      top: 17px; 
      right: 10px; 
      width: 0px; 
      height: 0px; 

      /* Creating the arrow using borders */ 
      border: 4px solid transparent; 
      border-left: 4px solid #fff; 
     } 

     /* Changing the color of the arrow on hover */ 
     #cssmenu > ul li>a:hover::after, #cssmenu > ul li:hover>a::after { 
      border-left: 4px solid #fff; 
     } 

     #cssmenu > ul li>a:hover::before, #cssmenu > ul li:hover>a::before { 
      border-left: 4px solid rgba(0, 0, 0, .25); 
     } 


     /* THE SUBMENUS */ 
     #cssmenu > ul ul { 
      position: absolute; 
      left: 150px; 
      top: -9999px; 
      padding-left: 5px; 
      opacity: 0; 
      /* The fade effect, created using an opacity transition */ 
      -webkit-transition: opacity .3s ease-in; 
      -moz-transition: opacity .3s ease-in; 
      -o-transition: opacity .3s ease-in; 
      -ms-transition: opacity .3s ease-in; 
     } 

     /* Showing the submenu when the user is hovering the parent link */ 
     #cssmenu > ul li:hover>ul { 
      top: 0px; 
      opacity: 1; 
     } 

在此先感謝您的幫助!

+1

菜單不在div中,因此鼠標事件當然會觸發。 JavaScript不知道這些元素是鏈接的。 – epascarello

+0

我嘗試過,但它是越野車,對不起,我還沒有明確說明我已經做了什麼......當我將菜單Div嵌套到Circle Div中時,它似乎在onmouseover ok上,但是當我移動鼠標時,它是越野車。 – user1360809

+0

當我嘗試在jsfiddle中使用您的代碼時,菜單遠離圓圈顯示。那是對的嗎?或者它應該在一個圓圈附近? –

回答

1

這裏有一個例子...

http://jsfiddle.net/5hnDn/5/

忘掉JS,不需要......

<div class="divCircle" id="divCircle"> 
<div id='cssmenu'>  
<ul> 
    <li class='active '><a href='index.html'><span>Home</span></a></li> 
    <li class='has-sub '><a href='#'><span>Products</span></a> 
     <ul> 
     <li class='has-sub '><a href='#'><span>Product 1</span></a> 
      <ul> 
       <li><a href='#'><span>Sub Item</span></a></li> 
       <li><a href='#'><span>Sub Item</span></a></li> 
      </ul> 
     </li> 
     <li class='has-sub '><a href='#'><span>Product 2</span></a> 
      <ul> 
       <li><a href='#'><span>Sub Item</span></a></li> 
       <li><a href='#'><span>Sub Item</span></a></li> 
       <li><a href='#'><span>Sub Item</span></a></li> 
      </ul> 
     </li> 
     </ul> 
    </li> 
    <li><a href='#'><span>About</span></a></li> 
    <li><a href='#'><span>Contact</span></a></li> 
</ul> 
</div> 
</div> 

...

只有改變規則,如下圖所示...

#divCircle { 
    position: relative; 
} 

#divCircle:hover #cssmenu { 
    display: block; 
} 
#cssmenu { 
    position: absolute; 
    top: 30px; 
    left: -60px; 
} 

#cssmenu > ul{ 
    padding: 10px 0 0 0; 
} 
+0

謝謝 - 我已經完成了這部分的確定,但我不確定如何保持菜單顯示,當我離開圓形到菜單上並使菜單消失時,我離開圓圈或菜單(如果這是有道理的!) – user1360809

+0

查看更新的答案... –

+0

謝謝 - 多數民衆贊成在完美,謝謝!你能解釋我出錯的地方嗎? – user1360809