2014-10-11 100 views
0

我試圖將鼠標懸停在圖像上方10px,但由於某種原因無法使用。 這是我曾嘗試:在鼠標上移動圖像

http://jsfiddle.net/w3qqv4vh/

CSS:

.image { 
    display: block; 
    position: absolute; 
    top: 0; 
    bottom: 0; 
    left: 0; 
    right: 0; 
    z-index: 2; 
    width: 238px; 
    height: auto; 
    margin-top: auto; 
    margin-right: auto; 
    margin-bottom: auto; 
    margin-left: auto; 
    overflow: hidden; 

.image:hover { 
    margin-bottom: 10px;  
    transition: margin-left .5s; 
    -moz-transition: margin-left .5s; /* Firefox 4 */ 
    -webkit-transition: margin-left .5s; /* Safari and Chrome */ 
    -o-transition: margin-left .5s; /* Opera */ 
} 
+0

http://jsfiddle.net/w3qqv4vh/2/ – 2014-10-11 19:27:06

+0

你的主圖像配類,將其更改爲顯示:內嵌塊,而位置:相對。之後,當你使用你的懸停事件做margin-top:-10px; – 2014-10-11 19:28:00

回答

1

有一些你需要對CSS的變化。

CSS

.image { 
    display: block; 
    position: absolute; 
    top: 0; 
    left: 0; 
    right: 0; 
    z-index: 2; 
    width: 238px; 
    height: auto; 
    margin-right: auto; 
    margin-bottom: auto; 
    margin-left: auto; 
    overflow: hidden; 
    } 

.image:hover { 
    margin-top: -10px;  
    transition: margin-left .5s; 
    -moz-transition: margin-left .5s; /* Firefox 4 */ 
    -webkit-transition: margin-left .5s; /* Safari and Chrome */ 
    -o-transition: margin-left .5s; /* Opera */ 
} 

http://jsfiddle.net/w3qqv4vh/1/

0

過渡應用於.image沒有.image:hover,只是使用transform

body { 
 
    background-color: rgb(255, 255, 255); 
 
    font-family: 'Source Sans Pro'; 
 
    font-size: 13px; 
 
    font-weight: 200; 
 
    line-height: 1.38; 
 
    color: #000000; 
 
} 
 

 

 
.image { 
 
    display: block; 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    left: 0; 
 
    right: 0; 
 
    z-index: 2; 
 
    width: 238px; 
 
    height: auto; 
 
    margin-top: auto; 
 
    margin-right: auto; 
 
    margin-bottom: auto; 
 
    margin-left: auto; 
 
    overflow: hidden; 
 
    -webkit-transform:translateY(0); 
 
    transition: transform .5s ease; 
 
} 
 
.image:hover { 
 
    -webkit-transform:translateY(-10px); 
 
}
<img class="image" alt="" src="http://lorempixel.com/output/food-q-c-284-239-1.jpg">

您可以使用填充太

body { 
 
    background-color: rgb(255, 255, 255); 
 
    font-family: 'Source Sans Pro'; 
 
    font-size: 13px; 
 
    font-weight: 200; 
 
    line-height: 1.38; 
 
    color: #000000; 
 
} 
 

 

 
.image { 
 
    display: block; 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    left: 0; 
 
    right: 0; 
 
    z-index: 2; 
 
    width: 238px; 
 
    height: auto; 
 
    margin: auto; 
 
    overflow: hidden; 
 
    transition: padding-bottom .5s ease; 
 
} 
 
    
 
.image:hover { 
 
    padding-bottom: 10px;  
 
    
 
}
<img class="image" alt="" src="http://lorempixel.com/output/food-q-c-284-239-1.jpg">

Jsfiddle