9

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

4

3 回答 3

15

Firefox 4+ 是唯一支持过渡伪元素的浏览器,例如:before:after

来源:http ://css-tricks.com/13555-transitions-and-animations-on-css-generated-content/

于 2011-09-05T10:56:20.067 回答
3

CSS 过渡在 WebKit 中仍处于试验阶段,因此您可能遇到了一些涉及 ::after 伪选择器的边缘情况错误。我建议完全避免它,而只是过渡border-color。这在 Chrome 和 Safari 中运行良好:

#small_gal div.p {
    border: 2px solid transparent;
    -webkit-transition: border-color 1s ease-in;
}

#small_gal div.p:hover {
    border-color: orange;
}
于 2011-09-05T11:00:39.233 回答
1

如果您可以在元素本身上定义属性并inherit在伪元素中使用,则可以在 :before 和 :after 等伪元素上使用 CSS 过渡。因此,在您的情况下opacity,您可以在border-color.

*, *:before, *:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

#small_gal div {
  border-color: transparent;
  cursor: pointer;
  display: inline-block;
  position: relative;
  -webkit-transition: border-color 0.5s ease-in-out;
  -moz-transition: border-color 0.5s ease-in-out;
  -o-transition: border-color 0.5s ease-in-out;
  -ms-transition: border-color 0.5s ease-in-out;
  transition: border-color 0.5s ease-in-out;
}
#small_gal div:after {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  border-color: inherit;
  border-style: solid;
  border-width: 3px;
  left: 0;
  top: 0;
}
#small_gal div:hover {
  border-color: #e27501;
}
#small_gal div img {
  display: block;
  height: auto;
  max-width: 150px;
  width: auto;
}
<div id="small_gal">
    <div><img src="//c2.staticflickr.com/6/5826/23633880170_4bb8492de8_z.jpg" /></div>
    <div><img src="//c2.staticflickr.com/6/5826/23633880170_4bb8492de8_z.jpg" /></div>
    <div><img src="//c2.staticflickr.com/6/5826/23633880170_4bb8492de8_z.jpg" /></div>
    <div><img src="//c2.staticflickr.com/6/5826/23633880170_4bb8492de8_z.jpg" /></div>
    <div><img src="//c2.staticflickr.com/6/5826/23633880170_4bb8492de8_z.jpg" /></div>
</div>

于 2015-12-23T18:05:01.220 回答