使用CSS过渡文本位置

时间:2014-09-22 13:20:54

标签: css css-transitions

我有一个包含4个相同图像的页面,每个图像在单独的div中占据页面的25%。在悬停时,会出现图像叠加,然后开始文本转换,这些都会正常运行。

问题是,我如何在每个图像(或div)的中心对齐文本,然后创建一个文本滑动到另一个位置的过渡?他们目前以我喜欢的方式为中心,但我不确定在哪里添加过渡......

我已经找到了以我想要的方式转换文本的方法,但我无法将其置于中心位置。任何帮助将非常感激。 JS Fiddle和下面的代码。

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>

1 个答案:

答案 0 :(得分:4)

您可以在.caption

的margin-right属性上添加转换
.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