动画改变单词宽度

Jos*_*osa 15 html javascript css jquery animation

我有一个句子,我在一个单词中淡入并用数组中的另一个单词替换它.然而,由于所有单词的长度都不同,所以句子宽度突然改变并且导致波动过渡.
如何设置宽度变化的动画?我尝试在css中添加一个到句子容器的转换,但是没有用.我将转换应用为1.5s all linear,因此无论何时发生变化,都应该为宽度以及其他所有内容设置动画.有任何想法吗?

$(function() {
  var hello = ['dynamic', 'a', 'aklsjdlfajklsdlkf', 'asdf'];
  var used = ['dynamic'];
  var greeting = $('#what');
  var item;

  function hey() {

    item = hello[Math.floor(Math.random() * hello.length)];
    if (hello.length != used.length) {
      while (jQuery.inArray(item, used) != -1) {
        item = hello[Math.floor(Math.random() * hello.length)];
      }
      used.push(item);
    } else {
      used.length = 0;
      item = hello[Math.floor(Math.random() * hello.length)];
      used.push(item);
    }
    greeting.html(item);
    greeting.animate({
      "opacity": "1"
    }, 1500);
  }

  window.setInterval(function() {
    greeting.animate({
      "opacity": "0"
    }, 1500);
    setTimeout(hey, 1500)
  }, 5000);

});
Run Code Online (Sandbox Code Playgroud)
#sentence {
  transition: 1.5s all linear;
}

#what {
  font-style: italic;
  text-decoration: underline;
  color: red;
}
Run Code Online (Sandbox Code Playgroud)
<p id="sentence">
  This is a sentence that has <span id="what">dynamic</span> text that alters width.
</p>

<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

编辑:对不起,如果我不清楚,我只想淡出这个词,而不是整个句子.我正在尝试设置宽度动画以适应新单词.我不想更改/添加任何元素,只需使用当前标记解决.

Rok*_*jan 29

使用Roko CB的jQuery淡化动画文本

function dataWord () {

  $("[data-words]").attr("data-words", function(i, d){
    var $self  = $(this),
        $words = d.split("|"),
        tot    = $words.length,
        c      = 0; 

    // CREATE SPANS INSIDE SPAN
    for(var i=0; i<tot; i++) $self.append($('<span/>',{text:$words[i]}));

    // COLLECT WORDS AND HIDE
    $words = $self.find("span").hide();

    // ANIMATE AND LOOP
    (function loop(){
      $self.animate({ width: $words.eq( c ).width() });
      $words.stop().fadeOut().eq(c).fadeIn().delay(1000).show(0, loop);
      c = ++c % tot;
    }());
    
  });

}

// dataWord(); // If you don't use external fonts use this on DOM ready; otherwise use:
$(window).on("load", dataWord);
Run Code Online (Sandbox Code Playgroud)
p{text-align: center;font-family: 'Open Sans Condensed', sans-serif;font-size: 2em;}

/* WORDS SWAP */
[data-words]{
  vertical-align: top;
  position: static;
}
[data-words] > span{
  position: absolute;
  color: chocolate;
}
Run Code Online (Sandbox Code Playgroud)
<link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>
  We provide
  <span data-words="code|solutions|design"></span>
  for your business.
</p>

<p>
  You ordered
  <span data-words="1|3|28"></span>
  <b>big</b>
  <span data-words="salad|macs|chips"></span>
</p>
Run Code Online (Sandbox Code Playgroud)

  • @JosueEspinosa是我们用来表示此变量是对HTMLElement对象的引用的常用方法.使代码更容易阅读.想象一下,你有`var width = $('#width')`,而你在第200行读到的是`width`,你可能认为它是一个数字.在代码中更容易阅读一个像`$ width`这样的变量 - 很清楚它是关于一个元素的.:) (4认同)