CSS:悬停过渡后
HTML 结构
<div id="small_gal">
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
</div>
图像有阴影,因此 border
不是解决方案,因为它将位于图像之外
边框正在过渡,在 FF 上可以顺利运行,但在 chrome 中则不行或者其他浏览器
这里是我使用的代码
#small_gal div:hover{cursor: pointer;}
#small_gal div:after {
content: '';
position: absolute;
width: 112px;
height: 81px;
border: 3px solid #e27501;
left: 9px; top: 9px;
z-index: 9;
opacity: 0;
-webkit-transition: opacity 0.5s ease-in-out;
-moz-transition: opacity 0.5s ease-in-out;
-o-transition: opacity 0.5s ease-in-out;
-ms-transition: opacity 0.5s ease-in-out;
transition: opacity 0.5s ease-in-out;
}
#small_gal div:hover:after {
opacity: 1;
}
注意:
#small_gal
只是容器 每个图像都包含在 div 中,因此我们可以实现 :after
The HTML structure
<div id="small_gal">
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
<div><img src="images1.jpg" /></div>
</div>
The images are having dropshadows so border
is not a solution, as it will be outside the image
The border is having transition and it works smoothly on FF but not in chrome or other browsers
Here is the code which I have used
#small_gal div:hover{cursor: pointer;}
#small_gal div:after {
content: '';
position: absolute;
width: 112px;
height: 81px;
border: 3px solid #e27501;
left: 9px; top: 9px;
z-index: 9;
opacity: 0;
-webkit-transition: opacity 0.5s ease-in-out;
-moz-transition: opacity 0.5s ease-in-out;
-o-transition: opacity 0.5s ease-in-out;
-ms-transition: opacity 0.5s ease-in-out;
transition: opacity 0.5s ease-in-out;
}
#small_gal div:hover:after {
opacity: 1;
}
Note:
#small_gal
is only the container
each image is wrapped in a div so we can implement :after
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Firefox 4+ 是唯一支持伪元素转换的浏览器,例如
:before
和:after
。来源:http://css-tricks.com/13555-transitions-and -animations-on-css- generated-content/
Firefox 4+ is the only browser that supports the transitioning of pseudo-elements such as
:before
and:after
.Source: http://css-tricks.com/13555-transitions-and-animations-on-css-generated-content/
CSS 转换在 WebKit 中仍处于实验阶段,因此您可能遇到了一些涉及 ::after 伪选择器的边缘情况错误。我建议完全避免它,而只是过渡
border-color
。这在 Chrome 和 Safari 中运行良好:CSS transitions are still experimental in WebKit so it's likely you've hit some edge-case bug involving the ::after pseudo-selector. I suggest avoiding it altogether and just transitioning
border-color
instead. This worked fine in Chrome and Safari:如果您可以在元素本身上定义属性并在伪元素中使用
inherit
,则可以在伪元素上使用 CSS 过渡,例如 :before 和 :after 。因此,在您的情况下,您可以在border-color
上放置过渡,而不是在opacity
上放置过渡。You can use CSS transitions on pseudo elements like :before and :after if you can define the property on the element itself and use
inherit
in the pseudo element. So in your case instead of putting a transition on theopacity
, you could put a transition on theborder-color
.