简体   繁体   English

CSS过渡无法在悬停上工作

[英]CSS Transition not working on hover

I am having an issue. 我有一个问题。 When I hover over the image, a dark overlay appears but CSS Transition property is not working on it. 当我将鼠标悬停在图像上时,会出现一个深色的叠加层,但CSS Transition属性无法对其进行处理。

Here is the html: 这是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>

And CSS is: 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%;
}

Here is the JSFiddle 这是JSFiddle

http://jsfiddle.net/cnbvoe32/ http://jsfiddle.net/cnbvoe32/

Any help would be highly appreciated. 任何帮助将不胜感激。

thanks in advance. 提前致谢。

It's because you can't transition from display: none to display: table . 这是因为您不能从display: none过渡到display: table

Instead, you could transition the opacity property by setting the initial display to table along with opacity: 0 and then transitioning to opacity: 1 : 相反,您可以通过将初始显示设置为table以及opacity: 0来转换opacity属性,然后再转换为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;
}

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

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