CSS:如何在文本更改时添加平滑移位到div宽度?

时间:2017-03-06 08:46:43

标签: javascript jquery css css-transitions transition

如果有人能比我更好地说出这个问题,请提出建议,我会改变(或自己编辑)。

这是我当前的jsfiddle:https://jsfiddle.net/5v7mzadu/

我的HTML:

<div class="text-cycler">
    WE <div class="c-text" id="ctext-1">CARE</div>
       <div class="c-text" id="ctext-2">THINK</div>
       <div class="c-text" id="ctext-3">SEE</div>
       <div class="c-text" id="ctext-1">KNOW</div>
</div>

我的CSS:

.text-cycler {
     text-align:center;
     font-size:25px;
}
.c-text {
    display:inline-block
}

我的Javascript:

var divs = $('div[id^="ctext-"]').hide(),
           i = 0;

(function cycle() { 

    divs.eq(i).fadeIn(400)
        .delay(1000)
        .fadeOut(400, cycle);

    i = ++i % divs.length;

})();

正如你所看到的那样,第二个词淡入/淡出。我想在div中添加一个平滑的过渡,这样div容器的宽度不会突然改变宽度大小。 (这样宽度&#34; snap&#34;更平滑)

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:3)

I believe you needed the animation over the content and text alignment center. And indeed this must solve your purpose.

I have added span{white-space: nowrap; vertical-align: text-top;} to force it to align in single line, and added jQuery animate method to animate the width of the rotating text

And here's the fiddle内点击按钮进行游戏

var divs = $('div[id^="ctext-"]').hide(),
    i = 0;

(function cycle() { 
  divs.eq(i)
  .animate(400, function(){
  	
    	$('.x').animate({width: $(this).innerWidth()});
    })
  	.fadeIn(400)
    .delay(1000)
    .fadeOut(400, cycle);
  i = ++i % divs.length;
})();
.text-cycler {
  text-align:center;
  font-size:25px;
}
span{white-space: nowrap; vertical-align: text-top;}
.c-text {
  display:inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text-cycler">
 <span> WE  </span><span class="x"><div class="c-text" id="ctext-1">CARE</div><div class="c-text" id="ctext-2">THINK</div><div class="c-text" id="ctext-3">SEE</div><div class="c-text" id="ctext-1">KNOW</div></span>

答案 1 :(得分:0)

请参阅此代码段

var divs = $('div[id^="ctext-"]').hide(),
    i = 0;

(function cycle() { 

  divs.eq(i).fadeIn(400)
    .delay(1000)
    .fadeOut(400, cycle);

  i = ++i % divs.length;

})();
.text-cycler {
  font-size:25px;
  position:fixed; /*added*/
  padding-left:40% /*added*/
}

.c-text {
  display:inline-block
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text-cycler" align="center">
WE <div class="c-text" id="ctext-1">CARE</div><div class="c-text" id="ctext-2">THINK</div><div class="c-text" id="ctext-3">SEE</div><div class="c-text" id="ctext-1">KNOW</div>
</div>

答案 2 :(得分:0)

JavascriptLess方式。

如果你想变得时髦。 (不用说,这是一个比可用的更时髦的解决方案)

.text-cycler {
  width:75%;
  margin:auto;
  user-select: none;
  text-align:center;
  font-size:5vw;
}
.text-cycler:after {
  content:"";
  display:inline-block;
  animation: change;
  animation-duration: 10s;
  animation-iteration-count: infinite;
}

@keyframes change {
  0% {
    content: "CARE";
    opacity: 0;
  }
  3% {
    opacity: 1;
  }
  22% {
    opacity: 1;
  }
  25% {
    content: "CARE";
    opacity: 0;
  }
  25.1% {
    content: "THINK";
  }
  28% {
    opacity: 1;
  }
  47% {
    opacity: 1;
  }
  50% {
    content: "THINK";
    opacity: 0;
  }
  50.1% {
    content: "SEE";
  }
  53% {
    opacity: 1;
  }
  72% {
    opacity: 1;
  }
  75% {
    content: "SEE";
    opacity: 0;
  }
  75.1% {
    content: "KNOW";
  }
  78% {
    opacity: 1;
  }
  97% {
    opacity: 1;
  }
  100% {
    content: "KNOW";
    opacity: 0;
  }
}
<div class="text-cycler">
  WE
</div>