简体   繁体   English

使边框和箭头在悬停时更改颜色

[英]Making a border and arrow change color on hover

I have created an image box when on fade, the border fades to red with a transition. 在淡入淡出时,我创建了一个图像框,边框随着过渡淡出为红色。 How do I make it fade back to the original colour when you move the mouse off of the div? 当您将鼠标移离div时,如何使它退回到原始颜色? It currently snaps back and doesn't do it with a transition. 当前,它会回弹,并且不会过渡。

Also, I have created an arrow which I would like placing over the image on the right hand side. 另外,我还创建了一个箭头,希望将其放置在右侧的图像上。 This needs to fade in the same way the border does when you hover over the image so that border & triangle both fade together. 当您将鼠标悬停在图片上时,边框需要以与边框相同的方式褪色,以使边框和三角形都一起褪色。

I am still fairly new to coding so apologies if this doesn't make sense. 我对编码如此陌生,如果这没有道理,我会道歉。

CSS CSS

.image {
position:relative;
width:200px;
height:200px;
border: solid 5px #3b3e40;
}
.image:hover {
border:solid 5px #ff0000;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
.image img {
width:100%;
vertical-align:top;
}
.arrow-left {
width: 0; 
height: 0; 
border-top: 25px solid transparent;
border-bottom: 25px solid transparent;  
border-right:25px solid #3b3e40; 
}
.arrow-left:hover {
width: 0; 
height: 0; 
border-top: 25px solid transparent;
border-bottom: 25px solid transparent;  
border-right:25px solid #ff0000;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}

HTML HTML

<div class="image">
<img src="http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg" alt="" />
<div class="arrow-left"></div>
</div>

<hr>

<div class="image">
<img src="http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg" alt="" />
<div class="arrowcontainer">
    <div class="arrow-left"></div>
</div>
</div>

http://jsfiddle.net/4fgnd/1112/ http://jsfiddle.net/4fgnd/1112/

Place the transition properties in the block for the element, not the :hover pseudo selector: 将过渡属性放在元素的块中,而不是:hover伪选择器:

.image {
    position:relative;
    width:200px;
    height:200px;
    border: solid 5px #3b3e40;
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}

Have the arrow change when the image is hovered, not itself: 将鼠标悬停在图像上时,更改箭头,而不是更改其本身:

.image:hover .arrow-left {
    width: 0; 
    height: 0; 
    border-top: 25px solid transparent;
    border-bottom: 25px solid transparent;  
    border-right:25px solid #ff0000;
}

JSFiddle 的jsfiddle

我会尝试将鼠标悬停在左箭头,而不是独立箭头。

.image:hover .arrow-left { width: 0; height: 0; border-top: 25px solid transparent; border-bottom: 25px solid transparent;
border-right:25px solid #ff0000;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM