CSS 如何在没有 jQuery 的情况下悬停时更改 div 背景图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5361888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:58:48  来源:igfitidea点击:

How to change a div background image while hover without jQuery?

cssimagebackground

提问by Selvin

I want to change the background of a DIV on hover event. Is there a way to do this using pure CSS?

我想在悬停事件上更改 DIV 的背景。有没有办法使用纯 CSS 来做到这一点?

回答by David says reinstate Monica

Yeah, this is pretty easy with pure CSS, albeit IE6, if I remember rightly, doesn't support :hoveron any elements except for a. But the following should work consistently in other browsers:

是的,使用纯 CSS 这很容易,尽管 IE6,如果我没记错的话,不支持:hovera. 但是以下内容在其他浏览器中应该一致:

div {
    background: #fff url(path/to/image.png) 0 0 no-repeat;
}

div:hover {
    background: #ffa url(path/to/hoverImage.png) 0 0 no-repeat;
}