html - CSS Transition 在悬停时不起作用

标签 html css hover css-transitions

我有一个问题。当我将鼠标悬停在图像上时,会出现一个深色覆盖层,但 CSS Transition 属性对其不起作用。

这是 html:

<div class="col-md-12">
    <div class="collection-category">
        <img src="http://dummyimage.com/600x400/#C1C1C1/fff" />
        <a href="#">
            <div class="overlay_wrap">
                <div class="overlay">
                    <h2 class="collection_title">Headline One</h2>
                </div>
            </div>
        </a>
    </div>
</div>

CSS 是:

.collection-category {
    display: block;
    overflow: hidden;
    margin: 10px 0;
    position: relative;
}

.collection-category img {
    width: 100%;
    max-height: 250px;
}

.collection_title {
    display: table-cell;
    vertical-align: middle;
    font-size: 28px;
}

.overlay_wrap {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;

    -moz-transition: all .2s ease-in-out;
    -webkit-transition: all .2s ease-in-out;
    -ms-transition: all .2s ease-in-out;
    -o-transition: all .2s ease-in-out;
    transition: all .2s ease-in-out;    
}
.overlay {
    display: none;
    background: rgba(2, 2, 2, 0.61);
    color: #FFF;
    text-align: center;
    -moz-transition: all .2s ease-in-out;
    -webkit-transition: all .2s ease-in-out;
    -ms-transition: all .2s ease-in-out;
    -o-transition: all .2s ease-in-out;
    transition: all .2s ease-in-out;    
}
.collection-category:hover>a>.overlay_wrap>.overlay {
    display: table;
    height: 100%;
    width: 100%;
}

这是 JSFiddle

http://jsfiddle.net/cnbvoe32/

任何帮助将不胜感激。

提前致谢。

最佳答案

这是因为您不能从display: none 转换到display: table

相反,您可以通过将初始显示设置为 table 以及 opacity: 0 然后转换为 来转换 opacity 属性>不透明度:1:

Updated Example

.overlay {
  display: table;
  background: rgba(2, 2, 2, 0.61);
  color: #FFF;
  text-align: center;
  -moz-transition: all .2s ease-in-out;
  -webkit-transition: all .2s ease-in-out;
  -ms-transition: all .2s ease-in-out;
  -o-transition: all .2s ease-in-out;
  transition: all .2s ease-in-out;
  opacity: 0;
  height: 100%;
  width: 100%;
}
.collection-category:hover>a>.overlay_wrap>.overlay {
  opacity: 1;
}

关于html - CSS Transition 在悬停时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33701531/

相关文章:

悬停图像灰度 + <p> 仅在悬停时可见

html - 我想为这个标志制作动画,但由于某种原因它不起作用

css - Z-index with before 伪元素

css - 使用 CSS 设置子级 <div> 的父级 <div> 宽度

css - 如何将 td 内的内容水平放置在单元格中心?

javascript - 文本被隐藏,需要时间才能在 Webkit 中使用 slider 显示

html - 为什么我的图像在悬停时不透明度没有变化?

css - 使用 css 将鼠标悬停在按钮上时如何更改网站背景图片?

javascript - HTML5 Canvas 绘制 tileset

javascript - else if 不打印控制台值