【问题标题】:Animation synching, cursor and highlight动画同步、光标和高亮
【发布时间】:2012-12-08 09:35:17
【问题描述】:

所以我几乎可以让我的code 以我想要的方式工作,但我的动画无法正确同步。我正在尝试为突出显示文本的光标设置动画,然后单击一个按钮。问题是光标太慢或太快。我正在尝试使这个动态,以便无论文本有多长,我仍然可以让动画同步。我知道这可能只是一个数学问题,但我无法完全理解它。试图用毫秒匹配像素的事情让我头晕目眩。在我拔掉我所有的头发之前请帮忙。谢谢。

这里是html

<p><span id="container">I need to be highlighted one character at a time</span>
<input id="click" type="button" value="click me"/></p>
<img src="http://dl.dropbox.com/u/59918876/cursor.png" width="16"/>

这里是 CSS

#container{
   font-size: 16px;  
   margin-right: 10px;   
}
.highlight{
    background: yellow;           
}
img{
  position: relative;
  top: -10px;    
} 

还有 javascript

function highlight(){
    var text = $('#container').text(); //get text of container
    $('#click').css('border','none'); //remove the border
    $('img').css('left', '0px'); //reset the cursor left
    $('img').animate({left: $('#container').width() + 40}, text.length*70); //animation of cursor
    $('#container').html('<span class="highlight">'+text.substring(0,1)+'</span><span>'+text.substring(1)+'</span>'); //set the first html
    (function myLoop (i) {//animation loop          
       setTimeout(function () {        
         var highlight = $('.highlight').text();
         var highlightAdd = $('.highlight').next().text().substring(0,1);;
         var plain = $('.highlight').next().text().substring(1);
         $('#container').html('<span class="highlight">'+highlight+highlightAdd+'</span><span>'+plain+'</span>');     
         if (--i) myLoop(i);//  decrement i and call myLoop again if i > 0
        }, 70)
    })(text.length);
    setTimeout(function () {   
        $('#click').css('border','1px solid black');
     }, text.length*85);
}

highlight();
var intervalID = setInterval(highlight, $('#container').text().length*110);
//clearInterval(intervalID);  

这是我一直在玩的fiddle的链接。

【问题讨论】:

    标签: javascript jquery html css animation


    【解决方案1】:

    我意识到这有点晚了,但这里有一些帮助(供将来参考)。 JQuery 的animate 函数默认设置为swingeasing,这意味着动画的速度将始终变化(参见here)。

    为了(某种程度上)解决问题,我在 animate 方法中为光标添加了 linear 选项,并略微提高了它的速度。

    你可以在JSFiddle看到这个新版本。

    但是,由于某些原因,setTimeout 循环可能会变慢,因此动画可能不同步。

    【讨论】:

      【解决方案2】:

      感谢 Dejo,我能够修改我的 code 以使其完全按照我的意愿工作。增加一个跨度的宽度比尝试扩大一个跨度同时缩小另一个跨度要容易得多。这也让我可以让光标移动和跨度宽度增加动画同步运行。

      HTML

      <p><span id="highlight"></span><span id="container">I need to be highlighted one character at a time</span><input id="click" type="button" value="click me"/></p>
      <img id="cursor" src="http://dl.dropbox.com/u/59918876/cursor.png" width="16"/>
      

      CSS

      p{
        position: relative;
        font-size: 16px;   
       }
      #highlight{
       position: absolute;
       background-color:yellow;
       height:20px;
       z-index:-50;   
      }
      #cursor{
       position: relative;
       top: -10px;    
      }
      #click{
        margin-left; 10px;   
      }
      

      还有 javascript

      function highlight(){
          var textLength = $('#container').text().length;
          $('#click').css('border','none'); //remove the border
          $('#cursor').css('left', '0px'); //reset the cursor left
          $('#highlight').width(0);
          $('#highlight').animate({width: $('#container').width()}, textLength * 70);
          $('#cursor').animate({left: '+='+$('#container').width()} , textLength * 70, function(){
              $('#cursor').animate({left: '+=30'} , textLength * 20);
          });
          setTimeout(function () {   
              $('#click').css('border','1px solid black');
           }, textLength*100);
      }
      
      highlight();
      var intervalID = setInterval(highlight, $('#container').text().length*120);
      //clearInterval(intervalID); 
      

      【讨论】:

        【解决方案3】:

        这可能会让我被否决,但也许你会得到一些更好的主意......
        Fiddle Here

        $(document).ready(function() {
        $('p').click(function(){
        
            $('span').animate({'width':'100'},1000);
            $('.cursor').animate({marginLeft: 100},1000);
        });
        });
        

        【讨论】:

        • +1 我喜欢它,并会看看我是否可以将它集成到我的代码中。我还是想看看有没有更好的解决方案。谢谢
        • 你摇滚!谢谢你的主意。这正是我想出解决方案所需要的。查看我更新的code
        猜你喜欢
        • 1970-01-01
        • 2013-07-17
        • 2021-12-05
        • 2019-11-11
        • 2011-02-07
        • 2014-08-19
        • 1970-01-01
        • 2014-07-04
        • 2019-10-27
        相关资源
        最近更新 更多