css - 如何仅使用 CSS 在悬停时进行无闪烁图像切换?

标签 css

代码如下:

<div id="compv-navbar">
        <a href="#"><img src="image1.png" id="icon1"></a> | 
        <a href="#"><img src="image2.png" id="icon2"></a> | 
        <a href="#"><img src="image3.png" id="icon3"></a> | 
        <span id="view_name"> Random Text</span>
 </div>

假设每个图像都有另一个后缀为“-hover”的图像,该图像将在悬停时被替换。

最佳答案

这是我会做的:

<div id="compv-navbar">
        <a href="#" id="icon1"></a> | 
        <a href="#" id="icon2"></a> | 
        <a href="#" id="icon3"></a> | 
        <span id="view_name"> Random Text</span>
</div>

a#icon1{
background:url("image1.png") no-repeat;
}
a:hover#icon1{
background:url("image1-hover.png") no-repeat;
}
a#icon2{
background:url("image2.png") no-repeat;
}
a:hover#icon2{
background:url("image2-hover.png") no-repeat;
}
a#icon3{
background:url("image3.png") no-repeat;
}
a:hover#icon3{
background:url("image3-hover.png") no-repeat;
}

关于css - 如何仅使用 CSS 在悬停时进行无闪烁图像切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3376484/

相关文章:

html - 为什么样式为按钮的 a 元素与按钮元素的行为不同?

javascript - 单击时将事件/选定状态添加到链接

css - 如何在 ScalaFX 中使用 CSS 设置自定义 TreeCell 的样式?

javascript - 如何使用 jQuery 切换 flexbox 样式弹出窗口的可见性?

html - 如何让 Text-Shadow 带有 Webkit 渐变

html - 使用线性渐变 &::before::after 在 css 中创建的背景图像不会随着网页展开

CSS 下拉菜单 : Third-level list doesn't hide after leaving through second-level

html - 如何停止打印媒体的 Bootstrap 表背景颜色设置覆盖我的类(class)设置

jquery - 由于动画期间的延迟加载,CSS 过渡不稳定。如何避免这种情况?

html - 仅使用 CSS(无 JavaScript)的输入字段中的占位符文本