【问题标题】:Rotating text with Javascript使用 Javascript 旋转文本
【发布时间】:2014-07-18 06:53:19
【问题描述】:

我想循环浏览一组创建文本旋转效果的单词。我的大部分工作都按预期工作。有什么方法可以在 p 元素的长度上使用 css 过渡?

当从 char.length > 10 的对象遍历到 char.length

HTML:

<p><span id="description-rotate"></span> something built on something else.</p>

SASS:

@-webkit-keyframes rotate-text

    0%
        opacity: 0

    30%
        opacity: 1

    50%
        opacity: 1

    70%
        opacity: 1


    100%
        opacity: 0

p
    font-family: 'Helvetica', sans-serif

.rotate-text
   -webkit-animation: rotate-text 3050ms cubic-bezier(0.645,  0.045, 0.355, 1.000) infinite
    -moz-animation: rotate-text 3050ms cubic-bezier(0.645,  0.045, 0.355, 1.000) infinite
    -o-animation: rotate-text 3050ms cubic-bezier(0.645,  0.045, 0.355, 1.000) infinite
    animation: rotate-text 3050ms cubic-bezier(0.645,  0.045, 0.355, 1.000) infinite

Javascript:

var descriptionArray = ['some text', 'some more text', 'some even longer text'];
var descriptionLength = descriptionArray.length;
var description = $('#description-rotate');

function loop(i) {
     description.text(descriptionArray[i%descriptionLength]);
     setTimeout(function() {
        loop(i+1);
        description.addClass('rotate-text');
    }, 3050); // This duration must match the length of the animation
}

loop(0);

我意识到这可能无法很好地解释我的目标,请查看 CodePen 以更好地了解我正在尝试创建的内容。

谢谢!

见:http://codepen.io/anon/pen/JueGx

【问题讨论】:

  • 请在此处发布您的代码,以便 SO 的当前和未来访问者可以沉浸在其所有缩进的荣耀中!
  • @JayBlanchard 完成 :)
  • @ColeRoberts 你想要这样的东西吗? stackoverflow.com/questions/20695877/…

标签: javascript jquery css rotation fade


【解决方案1】:

使用 jQuery 的简单示例
是将所需的循环/交换单词存储到data-*属性中:

$("[data-words]").attr("data-words", function(i, words) {

    var $self = $(this).text(""),
        words = words.split("|"),
        tot   = words.length,
        c     = 0; 

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

    var $words = $self.find("span");

    (function loop(){
      $self.animate({ width: $words.eq( c ).width() });
      $words.stop().fadeOut().eq(c).fadeIn().delay(1000).show(0, loop);
      c = ++c % tot;
    }());
    
});
/* DATA-WORDS Loop/swap words in sentence */
[data-words] {
  vertical-align: top;
  position: static;
}
[data-words] > span {
  display: none;
  position: absolute;
  color: #0bf;
}
<p>
  This is <span data-words="some|an interesting|some longer">some</span> text
</p>

<p>
  Say <span data-words="hi|wow">hi</span> to <span data-words="Javascript|Stack Overflow">mom</span>
</p>


<script src="//code.jquery.com/jquery-3.1.0.js"></script>

  • | 分隔的单词将转换为数组,最后转换为子 &lt;span&gt; 元素
  • 这样的span元素需要绝对定位在父span内部
  • jQuery 将初始化一个递归循环,计算下一个单词的宽度,并对其进行动画处理(淡入淡出 + 宽度)

【讨论】:

  • 这有点奇怪,因为$("[data-words]").attr() 的用法是使用设置 data-words 属性的语法。因此,通过这样做,您实际上是在改变 data-words 属性的值,如果您稍后尝试使用它可能会产生奇怪的副作用。您应该只是访问该值,而不是运行回调来修改它。如果它打算循环遍历每一个,则应使用.each()。
【解决方案2】:

jQuery 的animate() 函数怎么样? http://api.jquery.com/animate/

您可以为数组中的每个单词触发动画。这是一个想法,您必须弄清楚如何填充变量hasMoreWordsInTheArray 和nextWordInTheArray:

function animateParagraphTag(word) {
    if(hasMoreWordsInTheArray) {
        //some special code to calculate the best width based on the word's length (theNewWidthValue)
        $('p').animate(
            { width: theNewWidthValue },
            "fast",
            animateParagraphTag(nextWordInTheArray)
        );
    }
}

你需要根据单词的长度来计算宽度,并将其放在动画的参数中,然后,一旦 p 标签完成相应的扩展/收缩,它就会触发回调函数,你可以移动到数组的下一个元素(单词)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 2015-11-08
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多