由文本鼠标悬停触发的基于 CSS 的横幅图像交换
当用户将鼠标悬停在菜单文本上时,我希望将导航栏上方横幅上的图像背景替换为每个菜单项的特定图像。
我希望每个菜单项都会导致横幅将背景交换为与每个菜单项的文本相关的图像。
我只想使用 CSS...而不是 JavaScript。
这似乎是我通过谷歌搜索可以找到的最接近的东西,但正如你所看到的,它与我正在寻找的东西有点不同:
<div class="nav">
<a href="#">
<img src="logo.gif" width="187" height="136" alt="" />
</a>
</div>
以及与之配套的CSS:
div.nav {
height: 187px;
width: 136px;
margin: 0;
padding: 0;
background-image: url("logo-red.gif");
}
div.nav a, div.nav a:link, div.nav a:visited {
display: block;
}
div.nav img {
width: 100%;
height: 100%;
border: 0;
}
div.nav a:hover img {
visibility: hidden;
}
I'm looking to replace an image background on a banner above my navigation bar with a specific image for each menu item when the user rolls over the menu text.
I want it so that each menu item causes the banner to swap the background for an image related to the text of each menu item.
I only want to use CSS... Not JavaScript.
This seems like the closest thing I could find through some googling, but as you can see, it's a bit different from what I'm looking for:
<div class="nav">
<a href="#">
<img src="logo.gif" width="187" height="136" alt="" />
</a>
</div>
And the CSS to go along with it:
div.nav {
height: 187px;
width: 136px;
margin: 0;
padding: 0;
background-image: url("logo-red.gif");
}
div.nav a, div.nav a:link, div.nav a:visited {
display: block;
}
div.nav img {
width: 100%;
height: 100%;
border: 0;
}
div.nav a:hover img {
visibility: hidden;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够这样做。这应该可以实现您所寻求的。社区中的其他人可能有更好的方法,但这是我发现有效的方法。设置元素的样式等由您负责:)
我的 jsFiddle 位于此处:
http://jsfiddle.net/ChadR/tM2Nr/
下载并托管此库,或添加它在你的头部标签之间。
https://ajax.googleapis.com/ajax/libs /jquery/1.8.3/jquery.min.js
HTML:
JavaScript:
CSS:
I was able to do it this way. This should accomplish what you're seeking. Others in the community might have a better method, but this is one I've found that works. Styling the elements, etc. is on you :)
My jsFiddle for it is located here:
http://jsfiddle.net/ChadR/tM2Nr/
Either download and host this library, or add it between your head tags.
https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
HTML:
JavaScript:
CSS: