html - 将鼠标悬停在图像上时更改 div 的背景图像

标签 html css image background

我有以下代码:

<!DOCTYPE html> <html >
 <head> 
    <style type="text/css"> 
        .imgBox {background: url(lock75.png) no-repeat; } 
        .imgBox:hover {background: url(lock75light.png) no-repeat; } 
    </style> 
</head> 
<body> 
    <div style="width:200px; height:200px; border:1px solid;" class="imgBox"> </div> 
</body>
</html> 

我的图片是 75x75,当我将鼠标悬停在边框上时会出现问题,我希望将鼠标悬停在图片上时更改图片。

注意:我需要一个与此类似的 html 结构。

编辑

我忘了说我需要图片集中在其他 div 之上,而我没有一定的图片空间。

谢谢

最佳答案

您需要有一个容器,可以在以下结构中同时容纳背景图像和您的内容(我知道您要求的内容与您发布的内容类似,但这是不可能的;好吧,但它会涉及 CSS3元素和 jQuery)

这是设置

body {
    padding:10px;
}
.img-box {
    max-width:200px;
    width:100%;
    border:1px solid #ddd;
}
.img-box .img {
    width:200px;
    height:200px;
    background: url(https://unsplash.it/200/200/?image=876) no-repeat;
    border-bottom:1px solid #ddd;
}
.img-box .img:hover {
    background: url(https://unsplash.it/200/200/?image=876&blur) no-repeat;
}
p { margin:5px } 
<div class="img-box">
    <div class="img" ></div>
    <p>other content here</p>
</div>

关于html - 将鼠标悬停在图像上时更改 div 的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33282149/

相关文章:

html - 在彩色 div/背景图像上叠加向下箭头

javascript - 删除循环中的动态图像

php - 显示图像时出错 - 从 PHP 和 MySQL 新手到 Ninja 的代码

ios - Safari:输入元素上的光标未呈现透明

java - 如何在 Java 中将图像存储到磁盘?

asp.net - 必填表单域通信

javascript - 在 mailto : action 上打开 Gmail

html - 如何在JSP 中的两个变量之间插入一个空格?

jquery - 从 body::after 中移除 CSS 属性

css:如何将 DOM 的位置粘贴到另一个在响应式设计中具有绝对位置的 DOM?