我試圖改變圖標的顏色從白色到黑色在我的導航欄上時,不僅在圖像上懸停的區域。當我將圖像懸停在圖像上時,我已經改變了圖像,但我想將其擴展到周圍的區域。任何幫助將不勝感激,我的代碼如下。如何在懸停在該區域時更改導航欄中圖標的顏色,而不僅僅是圖像。
HTML:
.navbar {
width: 100%;
height: auto;
text-align: center;
background-color: transparent;
overflow: auto;
display: block;
}
.navbar a {
width: 20%;
padding: 2px 0;
float: left;
transition: all 0.3s ease;
color: white;
font-size: 12px;
text-decoration: none;
display: block;
}
.navbar a:hover {
background-color: white;
color: black;
display: block;
}
.active {
background-color: #4CAF50;
}
.menubar {
width: 32px;
height: 32px;
}
<div class="navbar">
<a href="homepage.html">
<figure>
<img class="menubar" src="../images/icons/home.png" onmouseover="this.src='../images/icons/home_black.png'" onmouseout="this.src='../images/icons/home.png'" />
<figcaption>Homepage</figcaption>
</figure>
</a>
<a href="car.html">
<figure>
<img class="menubar" src="../images/icons/car.png" onmouseover="this.src='../images/icons/car_black.png'" onmouseout="this.src='../images/icons/car.png'" />
<figcaption>Cars</figcaption>
</figure>
</a>
<a href="motorbike.html">
<figure>
<img class="menubar" src="../images/icons/motorcycle.png" onmouseover="this.src='../images/icons/motorcycle_black.png'" onmouseout="this.src='../images/icons/motorcycle.png'" />
<figcaption>Motorcycles</figcaption>
</figure>
</a>
<a href="cycle.html">
<figure>
<img class="menubar" src="../images/icons/bicycle.png" onmouseover="this.src='../images/icons/bicycle_black.png'" onmouseout="this.src='../images/icons/bicycle.png'" />
<figcaption>Bicycles</figcaption>
</figure>
</a>
<a href="boat.html">
<figure>
<img class="menubar" src="../images/icons/boat.png" onmouseover="this.src='../images/icons/boat_black.png'" onmouseout="this.src='../images/icons/boat.png'" />
<figcaption>Boats</figcaption>
</figure>
</a>
</div>
CSS
做你正在嘗試你會需要另一種顏色的替代圖像。你可以看看使用像fontawesome或glyphicons這樣的字體圖標庫。我建議你將你的鼠標移動到CSS類可能使用jQuery來幫助 – happymacarts
@happymacarts在這個例子中他確實有替代圖像。重新閱讀這個問題,你會發現他想移動將'img src'交換到父元素的JavaScript,所以'a'可以在'img'而不是'img'上實現,以達到同樣的效果。首先,我會推薦拔出JS,以便它不再內聯來完成此操作。 –
@JonUleis我錯過了他擁有所有內聯js – happymacarts