2014-09-22 75 views
2

我有一個包含4個相同圖像的頁面,每個頁面佔用單獨頁面中25%的頁面。懸停時,會出現一個圖像疊加層,然後文本轉換也會開始,所有功能都正常運行。使用CSS過渡文本位置

問題是,我將如何去對齊我的文字在每個圖像(或div)的中心,然後創建一個文本滑動到另一個位置的過渡?他們目前以我想要的方式爲中心,但我不確定在哪裏添加過渡...

我已經找到了以我想要的方式過渡文本的方法,但是我無法首先讓它居中。任何幫助將非常感激。 JS小提琴和下面的代碼。

JS小提琴:http://jsfiddle.net/n39wkkb1/1/

CSS:

body { 
    background: url('http://www.bootply.com/assets/example/bg_blueplane.jpg'); 
    height:100%; 
    margin:0; 
    padding:0; 
} 
div.bg { 
    position:fixed; 
    width:50%; 
    height:50% 
} 
#nw { 
    background-image: url('clevelandnight.jpg'); 
    background-size:cover; 
} 
#ne { 
    top:0; 
    left:50%; 
    background-image: url('news1.jpg'); 
    background-size:cover; 
} 
#sw { 
    top:50%; 
    left:0; 
    background-image: url('drinks1.jpg'); 
    background-size:cover; 
} 
#se { 
    top:50%; 
    left:50%; 
    background-image: url('clevelandday.jpg'); 
    background-size:cover; 
} 
.overlay { 
    height:100%; 
    text-align:center; 
    -webkit-transition:opacity .4s ease-out, height .4s ease-out, background .4s ease-out; 
    -moz-transition:opacity .4s ease-out, height .4s ease-out, background .4s ease-out; 
    -o-transition:opacity .4s ease-out, height .4s ease-out, background .4s ease-out; 
    -ms-transition:opacity .4s ease-out, height .4s ease-out, background .4s ease-out; 
    transition:opacity .4s ease-out, height .4s ease-out, background .4s ease-out; 
} 
.bg:hover .overlay { 
    background:rgba(0, 0, 0, .75); 
    opacity: 1; 
    height:100%; 
} 
.caption { 
    font-family: 'Open Sans Condensed', sans-serif; 
    font-weight:100; 
    color:white; 
    font-size:36pt; 
    -webkit-transition:font-size .4s ease-out 1s, color .4s ease-out 1s; 
    -moz-transition:font-size .4s ease-out 1s, color .4s ease-out 1s; 
    -o-transition:font-size .4s ease-out 1s, color .4s ease-out 1s; 
    transition:font-size .4s ease-out 1s, color .4s ease-out 1s; 
} 
.bg:hover .caption { 
    color:#7D7D7D; 
    font-size:72px; 
} 

HTML:

<html> 
    <head> 
    <link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'> 
    <title>Craig Does Cleveland</title> 
    <link href='stylesheet2.css' rel='stylesheet' type='text/css'> 
    </head> 
    <body> 
    <div id='nw' class='bg'> 
     <div class='overlay'> 
      <span class='caption'>Night Life</span> 
     </div> 

    </div> 
    <div id='ne' class='bg'> 
     <div class='overlay'> 
     <span class='caption'>News</span> 
     </div> 
    </div> 
     <div id='sw' class='bg'> 
     <div class='overlay'> 
     <span class='caption'>Food & Drink</span> 
     </div> 
    </div> 
     <div id='se' class='bg'> 
     <div class='overlay'> 
     <span class='caption'>Events</span> 
     </div> 
    </div> 
    </body> 
</html> 
+0

對不起,我更新了我的JS小提琴剛纔。在文本轉換大小和顏色後,我希望它轉換位置 – czmudzin 2014-09-22 13:26:20

+0

您可以簡單地使用margin屬性來實現此目的http://jsfiddle.net/hbirjand/n39wkkb1/5/ – Hbirjand 2014-09-22 13:28:01

+0

好的,您想在哪裏轉換文本至? – 2014-09-22 13:28:20

回答

4

您可以在.caption保證金右屬性添加過渡:

.caption { 
    font-family: 'Open Sans Condensed', sans-serif; 
    font-weight:100; 
    color:white; 
    font-size:36pt; 
    -webkit-transition:font-size .4s ease-out 1s, color .4s ease-out 1s, margin-right .4s ease-out 1.4s; 
    -moz-transition:font-size .4s ease-out 1s, color .4s ease-out 1s, margin-right .4s ease-out 1.4s; 
    -o-transition:font-size .4s ease-out 1s, color .4s ease-out 1s, margin-right .4s ease-out 1.4s; 
    transition:font-size .4s ease-out 1s, color .4s ease-out 1s, margin-right .4s ease-out 1.4s; 
} 

DEMO

+1

+1,並在'bg'的'hover'上增加了'margin-right'。 – Harry 2014-09-22 13:36:44

+0

非常感謝你,這非常棒,它以一種非常可行的方式過渡到「文本左側」。我正在嘗試過渡到「正確的文本」,但我很滿意,請你看看我創建的主題 - http://stackoverflow.com/q/38622227/1828637 – Noitidart 2016-07-27 20:10:45