2016-03-21 63 views
1

首先你可以看看這兩個圖像,讓你明白。得到雙「onmouseover」Javascript

當不叮:http://s15.postimg.org/sn6rk45rf/not_Hover.png 當懸停:http://s16.postimg.org/yk6beg1ad/on_Hover.png

現在,當我有我的鼠標在圖像,無論圖像獲取按鈕。 但我只是希望每個圖像都有自己的鼠標懸停按鈕,其他圖像隱藏按鈕。

我真的不知道如何解決這個問題,我非常喜歡Javascript。

這是我的HTML/CSS/Javascript代碼。

var buttonNew = document.getElementsByClassName('buttonNewest'); 
 
var buttonRan = document.getElementsByClassName('buttonRandom'); 
 
function imageOver() { 
 
    for(var i = 0; i < buttonNew.length; i++) { 
 
     buttonNew[i].style.display = "block"; 
 
     buttonNew[i].style.animation = "moveButtonsRight 2s"; 
 
    } 
 
    for(var i = 0; i < buttonRan.length; i++) { 
 
     buttonRan[i].style.display = "block"; 
 
     buttonRan[i].style.animation = "moveButtonsLeft 2s"; 
 
    } 
 
} 
 

 
function imageLeave() { 
 
    for(var i = 0; i < buttonNew.length; i++) { 
 
     buttonNew[i].style.display = "none"; 
 
    } 
 
    for(var i = 0; i < buttonRan.length; i++) { 
 
     buttonRan[i].style.display = "none"; 
 
     } 
 
    }
.charSelect[role="Background"] { 
 
    width: 1600px; 
 
    min-height: 600px; 
 
    margin: 25px auto; 
 
} 
 

 
.charSelect[role="Background"] > h1 { 
 
    width: 300px; 
 
    margin: 0 auto; 
 
    border: dashed 2px rgba(255, 207, 0, 0.75); 
 
    text-align: center; 
 
    text-transform: uppercase; 
 
    font-size: 2.6em; 
 
    text-shadow: 2px 2px 3px rgb(0, 0, 0); 
 
} 
 

 
.charSelect[role="Characters"] { 
 
    position: relative; 
 
    display: inline-block; 
 
    width: 250px; 
 
    height: auto; 
 
    background: rgba(42, 42, 42, 0.7); 
 
    border: dashed 2px rgba(255, 207, 0, 0.4); 
 
    color: rgba(255, 207, 0, 1); 
 
    opacity: 0.6; 
 
    -webkit-transition: opacity 1s; 
 
    margin-left: 250px; 
 

 
} 
 
.charSelect[role="Characters"]:hover { 
 
    opacity: 1; 
 
    transform: scale(1.05); 
 
} 
 

 
.charSelect[role="Names"] { 
 
    width: 100%; 
 
    font-size: 1.8em; 
 
} 
 

 
.charSelect[role="Names"] > p { 
 
    margin: 0 !important; 
 
    text-align: center; 
 
    text-transform: uppercase; 
 
    text-shadow: 1px 1px 2px rgb(0, 0, 0); 
 
} 
 

 
/* Buttons */ 
 
.charSelect[role="LatestVid"], .charSelect[role="RandomVid"] { 
 
    width: 170px; 
 
    height: 45px; 
 
    background: -webkit-linear-gradient(top, rgb(255, 207, 0), rgba(255, 207, 0, 0)); 
 
    text-align: center; 
 
    line-height: 45px; 
 
    color: black; 
 
    -webkit-transition: background 1s; 
 
    transition: background 1s; 
 
    box-shadow: 0px 0px 3px; 
 
} 
 
.charSelect[role="LatestVid"] { 
 
    display: none; 
 
    position: absolute; 
 
    top:50%; 
 
    right: 70%; 
 
} 
 

 
.charSelect[role="RandomVid"] { 
 
    display: none; 
 
    position: absolute; 
 
    top:50%; 
 
    left: 70%; 
 
} 
 
.charSelect[role="RandomVid"]:hover , .charSelect[role="LatestVid"]:hover { 
 
    background: rgb(255, 207, 0); 
 
} 
 

 

 
/* Animation */ 
 

 
@-webkit-keyframes moveButtonsLeft { 
 
    0% { 
 
    left: 50%; 
 
    } 
 
    100% { 
 
    left: 70%; 
 
    } 
 
} 
 

 
@-webkit-keyframes moveButtonsRight { 
 
    0% { 
 
    right: 50%; 
 
    } 
 
    100% { 
 
    right: 70%; 
 
    } 
 
}
<!-- Character one --> 
 
    <div onmouseover="imageOver()" onmouseleave="imageLeave()" class="charSelect" role="Characters"> 
 
     <img src="chars/Dekker.gif" width="250"/> 
 
     <a href="#"> <div class="charSelect buttonNewest" role="LatestVid">Newest Videos</div> </a> 
 
    <a href="#"> <div class="charSelect buttonRandom" role="RandomVid">Random Videos</div> </a> 
 
     <div class="charSelect" role="Names"><p>Dekker</p></div> 
 
    </div> 
 

 

 

 
    <!-- Character two --> 
 
    <div onmouseover="imageOver()" onmouseleave="imageLeave()" class="charSelect" role="Characters"> 
 
    <img src="chars/Dekker.gif" width="250"/> 
 
    <a href="#"> <div class="charSelect buttonNewest" role="LatestVid">Newest Videos</div> </a> 
 
    <a href="#"> <div class="charSelect buttonRandom" role="RandomVid">Random Videos</div> </a> 
 
    <div class="charSelect" role="Names"><p>Dekker</p></div> 
 
</div>

+0

我不知道爲什麼你使用JS ......如果我在讀你的問題很好,這可以全部用CSS來完成。 –

+0

嘗試學習庫,如基因敲除js或angularjs這種功能,因爲在將來,它們會變得有用。 – dpaul1994

回答

0

的問題是,你不reffering TOT你對光標當前對象。如果將光標移至圖像上,則您的功能將應用可在頁面上找到的所有buttonNewbuttonRan的更改。

+0

謝謝dpaul,我想學習Javascript,所以謝謝你的回答! –

2

您打電話給imageOver()即可循環使用所有您的元素。

而不是使用JS(所有)我會去用純CSS的:

*{font: 14px/1 sans-serif;} 
 

 
.charSelect{ 
 
    position: relative; 
 
    display: inline-block; 
 
    vertical-align: top; 
 
} 
 

 
.charButtons{ 
 
    position: absolute; 
 
    bottom: 40px; 
 
    width:  100%; 
 
    text-align:center; 
 
    opacity: 0; 
 
    visibility: hidden; 
 
      transition: 0.4s; 
 
    -webkit-transition: 0.4s; 
 
} 
 
.charButtons a{ 
 
    display: block; 
 
    margin-top: 1px; 
 
    text-align: center; 
 
    color:  #fff; 
 
    background: #444; 
 
    padding: 10px; 
 
    opacity: 0.9; 
 
      transition: 0.3s; 
 
    -webkit-transition: 0.3s; 
 
} 
 
.charButtons a:hover{ opacity:1; } 
 
.charSelect:hover .charButtons{ 
 
    visibility: visible; 
 
    opacity: 1; 
 
}
<div class="charSelect"> 
 
    <img src="http://placehold.it/180x150/4af/&text=Hero+1"> 
 
    <div class="charButtons"> 
 
    <a href="#">Newest Videos</a> 
 
    <a href="#">Random Videos</a> 
 
    </div> 
 
    <h2>HERO 1</h2> 
 
</div> 
 

 
<div class="charSelect"> 
 
    <img src="http://placehold.it/180x150/fa4/&text=Hero+2"> 
 
    <div class="charButtons"> 
 
    <a href="#">Newest Videos</a> 
 
    <a href="#">Random Videos</a> 
 
    </div> 
 
    <h2>HERO 2</h2> 
 
</div>

+0

謝謝你的回答! –

+0

@MichelWarStompSchmidt不客氣! –