我在将鼠标悬停在图像包含的 div 上时试图交换图像时遇到了麻烦。
我可以用 css 做到这一点,但我有 6 个 div,其中包含 6 个图像。如果我在 css 中这样做,我将需要 6 组悬停事件来更改背景图像。我想用最简单的代码来做到这一点。
目前图像未设置为背景图像,而是设置为 <img src>
所有悬停图像都使用相同的名称,但在文件名中添加了“over”。
即。 shop.png shopover.png 当悬停在其容器 div 上时,他们是否要附加 img src 文件名以添加“over”部分?
html:
<div class="row">
<a href="index.php?page=exhibitorlist&id=1"> <div class="c2 center"><img src="images/eliquid.png"><h2 class="center">Juice Brands</h2></div></a>
<a href="index.php?page=exhibitorlist&id=2"> <div class="c2 center"><img src="images/mod.png"><h2 class="center">Hardware</h2></div></a>
<a href="index.php?page=exhibitorlist&id=3"><div class="c2 center"><img src="images/dist.png"><h2 class="center">Distributors</h2></div></a>
<a href="index.php?page=exhibitorlist&id=4"><div class="c2 center on"><img src="images/shop.png"/><h2 class="center">Retailers</h2></div></a>
<a href="index.php?page=exhibitorlist&id=5"><div class="c2 center"><img src="images/robot.png"><h2 class="center">Machinary / Packaging</h2></div></a>
<a href="index.php?page=exhibitorlist&id=6"><div class="c2 center"><img src="images/other.png"><h2 class="center">Other</h2></div></a>
</div>
样式:
#exhibitorcontainer {
background-color:rgba(0, 0, 0, 0.7);
background:url(../images/bg2.png) repeat;
width:100%;
height:100%;
}
#exhibitorcontainer .c2 {
margin: 0 5px;
width: 15.8%;
background-color:rgba(0, 0, 0, 0.5);
}
#exhibitorcontainer h2 {
font-size:16px;
color: #1b9bff;
}
#exhibitorcontainer .c2:hover h2 {
color:#666;
}
此刻我将 H2 从蓝色切换为灰色,我想对图像做同样的事情。关于执行此操作的最佳方法有什么想法吗?
最佳答案
最简单的方法是使用两张图片:
<div class="c2 center">
<img src="images/dist.png" class="normal-image">
<img src="images/dist-over.png" class="hover-image">
<h2 class="center">Distributors</h2>
</div>
然后使用CSS:
.hover-image, .c2:hover .normal-image {
display: none;
}
.c2:hover .hover-image {
display: inline;
}
关于javascript - 将鼠标悬停在其容器 div 上时交换图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41741776/