2016-10-04 31 views
0

我正在設計的網頁的一部分由幾個並排放置的div元素組成。每個包含一個圖像。我想在用戶的鼠標進入div時召喚一個下拉菜單。但是,使用簡單的「打印問候」功能進行測試並不會產生我期待的結果。onmouseenter事件不會在div元素上工作

在這一點上,圖像放置正確,他們的大小調整就像我指定的。但是,光標不是pointer,並且在鼠標進入div時功能test不運行。我在過去的點擊事件中遇到過這個問題,我一直試圖找到解決方法。有人可以解釋爲什麼它不起作用嗎?

這裏的HTML

#container { 
 
    background-color: black; 
 
    border-radius: 20px; 
 
    display: inline-block; 
 
    position: fixed; 
 
    width: 100%; 
 
    z-index: 2; 
 
    top: 0; 
 
} 
 
#icon { 
 
    background-color: burlywood; 
 
    border-radius: inherit; 
 
    border-radius: 20px; 
 
} 
 
#menu a { 
 
    font-size: 30px; 
 
    font-family: "Arial"; 
 
    color: white; 
 
    text-decoration: none; 
 
    margin-left: 50px; 
 
} 
 
#flag { 
 
    width: 50px; 
 
    height: 50px; 
 
} 
 
#menu a:hover { 
 
    color: #e55d00; 
 
} 
 
body { 
 
    height: 2000px; 
 
    background-image: url("background.jpg"); 
 
} 
 
#btt { 
 
    bottom: 0; 
 
    color: white; 
 
    position: fixed; 
 
    text-decoration: none; 
 
} 
 
#contain { 
 
    display: inline-block; 
 
    height: 1000px; 
 
    width: 100%; 
 
    max-width: 100%; 
 
    position: absolute; 
 
} 
 
#sidemenu { 
 
    width: 0; 
 
    height: 100%; 
 
    z-index: 1; 
 
    background-color: black; 
 
    transition: width 1s; 
 
    padding-top: 200px; 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
    overflow-x: hidden; 
 
} 
 
#sidemenu a { 
 
    padding: 8px 8px 8px 32px; 
 
    text-decoration: none; 
 
    font-size: 25px; 
 
    color: #818181; 
 
    display: block; 
 
    transition: 0.3s; 
 
} 
 
#sidemenu a:hover { 
 
    color: red; 
 
} 
 
#language { 
 
    margin-left: 250%; 
 
} 
 
#title a { 
 
    color: #e55d00; 
 
    text-decoration: none; 
 
} 
 
#title { 
 
    font-size: 50px; 
 
    text-align: center; 
 
    border-radius: 20px; 
 
    display: inline-block; 
 
    padding: 10px; 
 
} 
 
#projects { 
 
    margin-top: 200px; 
 
} 
 
#current { 
 
    width: 210px; 
 
    height: 200px; 
 
    cursor: pointer; 
 
} 
 
#current img { 
 
    width: inherit; 
 
    height: inherit; 
 
} 
 
#progress { 
 
    margin-left: 200px; 
 
    width: 210px; 
 
    height: 200px; 
 
    cursor: pointer; 
 
} 
 
#progress img { 
 
    width: inherit; 
 
    height: inherit; 
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Projects</title> 
 
    <link type="text/css" rel="stylesheet" href="projects.css"> 
 
</head> 
 

 
<body> 
 
    <div id="container"> 
 
    <table> 
 
     <tr> 
 
     <td> 
 
      <div id="icon"> 
 
      <img src="img.png"> 
 
      </div> 
 
     </td> 
 
     <td> 
 
      <div id="title" title="Home"> 
 
      <a href="electrocats.html">Electrocats</a> 
 
      </div> 
 
      <div id="menu"> 
 
      <a href="#" onclick="summonMenu()">News</a> 
 
      <a href="projects.html">Projects</a> 
 
      <a href="#">About Us</a> 
 
      <a href="#">menu item 4</a> 
 
      </div> 
 
     </td> 
 
     <td> 
 
      <div id="language"> 
 
      <a href="#" title="Translate to Greek"> 
 
       <img id="flag" src="Greece-icon.png"> 
 
      </a> 
 
      </div> 
 
     </td> 
 
     </tr> 
 
    </table> 
 
    </div> 
 

 
    <div id="contain"> 
 
    <div id="sidemenu" onmouseleave="hideMenu()"> 
 
     <a href="Contact.html">contact</a> 
 
     <a href="Films.html">Films</a> 
 
    </div> 
 
    </div> 
 

 
    <!-- here is the code with the problem --> 
 
    <div id="projects"> 
 
    <table> 
 
     <tr> 
 
     <td> 
 
      <div id="current" onmouseenter="test"> 
 
      <img src="high-voltage.png"> 
 
      </div> 
 
     </td> 
 
     <td> 
 
      <div id="progress" onmouseenter="test"> 
 
      <img src="engineering1.jpg"> 
 
      </div> 
 
     </td> 
 
     </tr> 
 
    </table> 
 
    </div> 
 

 

 

 
    <a id="btt" href="#top">Back to top</a> 
 

 

 
    <script> 
 
    function summonMenu() { 
 
     var menu = document.getElementById("sidemenu"); 
 
     menu.style.width = "400px"; 
 
    } 
 

 
    function hideMenu() { 
 
     var menu = document.getElementById("sidemenu"); 
 
     menu.style.width = "0"; 
 
    } 
 

 
    function test() { 
 
     console.log("hello"); 
 
    } 
 
    </script> 
 
</body> 
 

 
</html>

+2

您的控制檯是否返回任何錯誤? – Santi

+0

沒有任何錯誤或結果。 – VlassisFo

+0

工作得很好:https://jsfiddle.net/js8j0a1s/ - 有時控制檯需要一秒鐘才能顯示內容。 –

回答

0

最後,設置div的包含圖像absolute位置解決了問題。我還沒有理解爲什麼,但我相信它與在css文件中指定的z-index值有關。如果有人能夠解釋爲什麼這樣解決問題,我會非常感激。

2

您好,請參見下面的小提琴在這裏,我沒有這個使用alert但可以使用console.log希望它可以幫助你做的一樣。

Div Hover Fiddle

還加入了擺弄一個下拉列表,鼠標輸入可見事件你有什麼想提出我想是。

dropdown Fiddle

相關問題