【问题标题】:Change an element every few seconds and sync with animation每隔几秒更改一个元素并与动画同步
【发布时间】:2014-11-22 13:06:21
【问题描述】:

最近我一直在学习一些 JavaScript 来改进我的网页。

我尝试做一个简单的练习,其中我每 5 秒更改一个元素,但是当我尝试使用 css 添加淡入淡出动画并且仅适用于第一次时,此时我真的不知道我的 javascript没有以正确的方式编写或与我的 css 有关。

有更多知识和经验的人能给我一些建议吗?

好的,所以基本上问题是我不能让动画像句子一样重复,你可以在这里查看:

//Calling the element.
var $slogan = document.getElementById("slogan");

// Setting an array with several strings.
var sloganArray = ["This sentence will change every 5 seconds!", "See? I'm changing!", "Knock Knock.", "Who's there?", "Heck I don't know!"];

//Setting variable to control the index.
var sloganIndex = 0;

/* This function (only when called) replaces the text of the element called before with text contained on the strings of the array, each time incrementing one and going through every array position. */
function changeSlogan() {
    $slogan.innerHTML = sloganArray[sloganIndex];
    ++sloganIndex;
    if (sloganIndex >= sloganArray.length) {
        sloganIndex = 0;
    }
  }

//Calling the function created before every five seconds.
setInterval(changeSlogan,5000);
#slogan {
    margin: 0;
    font-family: Arial, sans-serif;
    font-size: 20px;
    color: #000;
    opacity: 1;
    transition: opacity .3s ease-out;
    -webkit-animation:fadeIn ease-out ;
    -moz-animation:fadeIn ease-out ;
    animation:fadeIn ease-out;
    -webkit-animation-fill-mode:forward;  /* this makes sure that after animation is done we remain at the last keyframe value (opacity: 1)*/
    -moz-animation-fill-mode:forward;
    animation-fill-mode:forward;
    -webkit-animation-duration: 1.5s;
    -moz-animation-duration: 1.5s;
    animation-duration: 1.5s;
    -webkit-animation-delay: 4.9s; /* Chrome, Safari, Opera */
    animation-delay: 4.9s;
    animation-iteration-count: infinite;
}
<h3 id="slogan"><em>This is a dynamic sentence!</em></h3>

感谢您的宝贵时间!

【问题讨论】:

    标签: javascript html css loops animation


    【解决方案1】:

    我想我会尝试使用requestAnimationFrame 来回答这个问题,这是一种使用 JavaScript 为元素设置动画的高效方法。它使用您机器的内部帧速率,这意味着它将使用更少的内存,并确保它与您添加的任何可能的 CSS 动画保持同步。 I've created a CodePen 还用 cmets 添加了下面的代码。

    // All your slogans
    const sloganArray = [
      "This sentence will change every 5 seconds!",
      "See? I'm changing!",
      "Knock Knock.",
      "Who's there?",
      "Heck I don't know!"
    ];
    
    // Initial slogan index
    let sloganIndex = 0;
    
    // Slogan container
    const element = document.getElementById("slogan");
    
    // Set first slogan on render
    element.innerHTML = sloganArray[sloganIndex];
    
    // The last interval in which something changed, starting from 0 and will be updated by the function
    let lastIntervalTimestamp = 0;
    
    // Start of function
    function render(now) {
      
      // If the interval is 0
      // or "right now" minus timestamp is equal or higher than 5 seconds
      // do the following
      if (!lastIntervalTimestamp || now - lastIntervalTimestamp >= 5 * 1000) {
        
        // Update the timestamp to right now
        lastIntervalTimestamp = now;
    
        // Update the slogan in the container
        element.innerHTML = sloganArray[sloganIndex];
        
        // Progress the slogan index along by 1
        ++sloganIndex;
        
        // If the slogan index is at the last of the slogan array flip to 0
        if (sloganIndex >= sloganArray.length) {
          sloganIndex = 0;
        }
      }
      
      // Rerun the whole function again when finished, this happens constantly while the animation API runs
      requestAnimationFrame(render);
    }
    
    // Start the function when the very first animation frame is requested, aka when the browser is ready
    window.requestAnimationFrame(render);
    

    【讨论】:

      【解决方案2】:

      我建议对您的情况使用 only css 和 no javascript。 我创建了三个效果。使用.slogan, .slogan up, .slogan down 并将变化的元素放入<p> 元素中,如下所示。

      检查和 css 在这里:http://jsfiddle.net/csdtesting/ncmctw65/1/

      要设置的 HTML:

      <div class="wrapper">
          <h3>Cool fading text only with css try .slogan, .sloga down, .slogan up </h3>
          <div class="slogan down">
              <p><a href="http://www.hongkiat.com/blog/wordpress-related-posts-without-plugins/">This sentence will change every 5 seconds!</a></p>
              <p><a href="http://www.hongkiat.com/blog/wordpress-related-posts-without-plugins/">See? I'm changing!</a></p>
              <p><a href="http://www.hongkiat.com/blog/automate-dropbox-files-with-actions/">Who's there?</a></p>
              <p><a href="http://www.hongkiat.com/blog/wordpress-related-posts-without-plugins/">eck I don't know!</a></p>
          </div>
      </div>
      

      希望你喜欢!

      【讨论】:

      • 哇,这真的很酷,做得很好。非常感谢,非常强大的动画,它帮助我更好地理解了一些 CSS 方面。但我现在真的专注于 Javascript,你认为这有解决方案还是结构不好?谢谢!
      • 如果你想试试 jquery 会容易得多。 jsfiddle.net/mdesdev/b3t5k 。或带有 css 的纯 javascript:jsfiddle.net/qrc8m.
      • 感谢您分享您的知识,这非常好。如果可以的话,当然给我一些建议,我应该先学习一点 javascript 还是以正确的方式进入 jQuery?再次感谢。
      • 我建议两者。从js开始,jquery会很容易学习。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 2014-01-08
      • 2012-10-30
      相关资源
      最近更新 更多