[英]Change background color and image on hover
I have a square with a logo inside. 我有一个带有徽标的正方形。 On hover in the square, I want the background color to change as well as the color of the logo.
悬停在广场上时,我希望背景颜色以及徽标的颜色发生变化。
I have the following code: 我有以下代码:
<div class="pure-u-1 pure-u-md-1-3">
<div class="project", id="project1">
<a href="#" ><img class="pure-img" src="/media/logo.png" onmouseover="this.src='/media/logo2.png'" onmouseout="this.src='/media/logo.png'" width="80px" height="78px" alt="logo"></a>
</div>
</div>
.project {
background-color: #f5f4f4;
margin: 0 0.5em 2em;
padding: 4em 4em;
}
#project1:hover {
background-color: red;
}
I can get the logo to change on hover and I can get the square to change, but I can't get them to both change at the same time, ie when the mouse touches the square. 我可以在悬停时更改徽标,也可以更改正方形,但是不能同时(即当鼠标触摸正方形时)更改它们。
I'm assuming this needs javascript, which I do not know. 我假设这需要不知道的JavaScript。 Any tips/help is greatly appreciated.
任何提示/帮助,不胜感激。
Cleaner HTML (since img tag needs a source, you can change it for a div): 更干净的HTML(由于img标签需要源代码,因此您可以将其更改为div):
<div class="pure-u-1 pure-u-md-1-3">
<div class="project", id="project1">
<div class="pure-img">
</div>
</div>
And the CSS: 和CSS:
.project {
background-color: #f5f4f4;
margin: 0 0.5em 2em;
padding: 4em 4em;
}
#project1:hover {
background-color: red;
}
.pure-img{
background-image: url(http://dummyimage.com/600x400/000/fff);
width: 80px;
height: 78px;
}
#project1:hover .pure-img {
background-image: url(http://dummyimage.com/600x400/666/0011fc);
}
and the fiddle http://jsfiddle.net/h6gwwox6/1/ 和小提琴http://jsfiddle.net/h6gwwox6/1/
No JavaScript required, just CSS. 不需要JavaScript,只需CSS。 No need for an
<img>
either. 也不需要
<img>
。
<div class="logo">Brand Name</div>
.logo {
width: 80px;
height: 78px;
text-indent: -9999em;
background-image: url('http://s17.postimg.org/7hltqe5e3/sprite.png');
background-repeat: no-repeat;
background-position: 0 0;
background-color: blue;
}
.logo:hover {
background-color: red;
background-position: -80px 0;
}
http://jsfiddle.net/12u7ma2q/ http://jsfiddle.net/12u7ma2q/
Create a sprite with both versions of the logo side-by-side. 并排使用两个版本的徽标创建一个精灵。 When you hover you will change the background color and shift the position of the sprite image (left, right, up, down - depends on how you created your sprite).
悬停时,您将更改背景颜色并移动精灵图像的位置(向左,向右,向上,向下-取决于创建精灵的方式)。
The benefits to this answer over sailens is that you're not using invalid markup ( <img>
without a src
attribute) and you're only making a single request for an image instead of two. 这个解决方案相对于sailens的好处在于,您没有使用无效的标记(没有
src
属性的<img>
),并且只对一个图像发出了一个请求,而不是两个。 Oh, and less markup - a single <div>
(which could be an <a>
, <span>
etc). 哦,还有更少的标记-一个
<div>
(可以是<a>
, <span>
等)。
You could also shorten .logo
by using background instead of individual background properties. 您还可以通过使用背景而不是单个背景属性来缩短
.logo
。 I listed them out at first for clarity. 为了清楚起见,我首先列出了它们。
.logo {
width: 80px;
height: 78px;
text-indent: -9999em;
background: blue url('http://s17.postimg.org/7hltqe5e3/sprite.png') no-repeat 0 0;
}
http://jsfiddle.net/12u7ma2q/1/ http://jsfiddle.net/12u7ma2q/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.