2014-09-22 71 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