2015-11-13 74 views
1

我遇到問題。當我將鼠標懸停在圖像上時,會出現黑色疊加層,但CSS Transition屬性無法處理它。CSS轉換不能用於懸停

下面是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/

任何幫助將高度讚賞。

在此先感謝。

回答

3

這是因爲你不能從display: none轉換到display: table

相反,你可以用opacity: 0沿設定初始顯示table,然後過渡到opacity: 1過渡opacity屬性:

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; 
} 
+0

感謝您的快速回復。該解決方案有效,但不是100%。過渡只適用於懸停,但不能懸停。在懸停時,文本在左上角顯示一段時間。 – absikandar

+0

@absikandar是的,我更新了它。現在應該是好的。 –

+0

你是神話般的。感謝你的幫助。解決方案就像魅力一樣。 – absikandar