2013-06-24 65 views
0

我在表格中有幾張圖片,它們用作鏈接並懸停在播放按鈕上方。如何讓許多圖像具有相同的懸停圖像

我嘗試了很多不同的技巧,但他們都有問題,工作不正常。我決定在這裏分享我的問題,找到一個標準的解決方案。

這是我迄今所做的:

img{ 
    position: relative; 
} 
img:hover:before { 
    background:url(http://i40.tinypic.com/i3s4dc.png) no-repeat center center; 
    content:""; 
    width: 100%; 
    min-height: 100px; 
    position: absolute; 
    left: 0; 
} 

我不知道如果我在正確的方向或沒有,但看看演示http://jsfiddle.net/jmXdh/8/,如果它是錯的,那麼請讓我知道任何其他方式。

+0

爲什麼不能你簡單嗎? y爲元素添加一個額外的類,只在懸停時加載背景圖像? – blackhawk

回答

2

這裏的東西可能工作,假設你可以添加額外的標記:

http://jsfiddle.net/isherwood/jmXdh/11/

a { 
    display: inline-block; 
    overflow: hidden; 
    position: relative; 
} 
a:hover .play { 
    background:url(http://placehold.it/80x80) no-repeat center center; 
    opacity: 0.8; 
    position: absolute; 
    width: 80px; 
    height: 80px; 
    left: 50%; 
    top: 50%; 
    margin-left: -40px; 
    margin-top: -50px; 
} 

<a href="/"> 
    <div class="play"></div> 
    <img class="img" src="http://i42.tinypic.com/2v9zuc1.jpg" /> 
    <br /> 
    <b>Video test</b> 
</a> 

或者與過渡效果:

http://jsfiddle.net/isherwood/jmXdh/12/

.play { 
    opacity: 0; 
    transition: opacity 0.3s; 
} 
a:hover .play { 
    opacity: 0.7; 
}