繁体   English   中英

使用jQuery编写完美动画的正确方法

[英]Proper way to write a perfect animation using jquery

当前场景:

我有一个divs列表,其中包含一组元素,这些元素会根据当前divs的数量自动滚动。 divs列表放置在parent div并具有固定的height

要求:

  • 如果内部存在的divs的长度小于4,请不要设置动画,即滚动。
  • 以相同的速度进行动画处理,直到最后一个div ,反之亦然。
  • 在任何子div悬停时停止滚动。

实现:

  • 完成滚动,即如果少于四个元素,则不滚动。
  • 滚动停止在任何子div悬停上。
  • 动画到底部元素,然后从头开始。

目前面临的问题:

  • 滚动不会以相同的速度发生。 它开始缓慢,增加速度,然后缓慢结束。
  • 悬停停止滚动后,再次启动时,它将再次缓慢启动。
  • 一旦到达终点,它将停留在那里几秒钟,然后从头开始。

示范

HTML

<div id="events_partial" class="container body-content col-md-12 col-lg-12 set-max-height">
    <div class="row sec-container" id="secContainer">
        <div style="top: -550.242px; opacity: 1;" id="secDetails">
            <div class="container">
                <p><h3><strong> Program in Place 1 &nbsp;on&nbsp;11/22/2015</strong></h3></p>
                <p>
                  Program Description which is a very long text on document and it  can be more than 2 lines
                </p>
            </div>
            <div class="container">
                <p><h3><strong> Program in Place 1 &nbsp;on&nbsp;11/22/2015</strong></h3></p>
                <p>
                  Program Description which is a very long text on document and it  can be more than 2 lines
                </p>
            </div>
            <div class="container">
                <p><h3><strong> Program in Place 1 &nbsp;on&nbsp;11/22/2015</strong></h3></p>
                <p>
                  Program Description which is a very long text on document and it  can be more than 2 lines
                </p>
            </div>

            <div class="container">
                <p><h3><strong> Program in Place 1 &nbsp;on&nbsp;11/22/2015</strong></h3></p>
                <p>
                  Program Description which is a very long text on document and it  can be more than 2 lines
                </p>
            </div>
            <div class="container">
                <p><h3><strong> Program in Place 1 &nbsp;on&nbsp;11/22/2015</strong></h3></p>
                <p>
                  Program Description which is a very long text on document and it  can be more than 2 lines
                </p>
            </div>
            <div class="container">
                <p><h3><strong> Program in Place 1 &nbsp;on&nbsp;11/22/2015</strong></h3></p>
                <p>
                  Program Description which is a very long text on document and it  can be more than 2 lines
                </p>
            </div>
            <div class="container">
                <p><h3><strong> Program in Place 1 &nbsp;on&nbsp;11/22/2015</strong></h3></p>
                <p>
                  Program Description which is a very long text on document and it  can be more than 2 lines
                </p>
            </div>
            <div class="container">
                <p><h3><strong> Program in Place 1 &nbsp;on&nbsp;11/22/2015</strong></h3></p>
                <p>
                  Program Description which is a very long text on document and it  can be more than 2 lines
                </p>
            </div>
      </div>
    </div>
</div>

CSS

#events_partial {
    min-height: 385px !important;
    margin-top: 57px;
    overflow: hidden;
}
.set-max-height {
    max-height: 385px !important;
    padding-top: 30px !important;
}

.sec-container {
    overflow: hidden !important;
    min-height: 200px;
}

#secDetails {
    position: absolute;
    margin-top: 0px;
}

JS

var animateTime = 50000; //kept varying time because as number of items increases the speed time decreased
var shouldAnimate = true;
if ($("#secDetails .container").length < 4) {
    shouldAnimate = false;
}
if ($("#secDetails .container").length >= 4 && $("#secDetails .container").length < 9)
    animateTime = 10000;
function marqueePlay() {
   if (shouldAnimate) {
       $("#secDetails").animate(
       {
            top: $("#events_partial").height() - $("#secDetails").height(),
            opacity: 1
       }, animateTime, function () {
             $("#secDetails").css("top", 1);
             $("#secDetails").css("opacity", 1);
             marqueePlay();
       });
    }
}
marqueePlay();
$("#secDetails").hover(function () {
    $(this).stop(); //Stop the animation when mouse in
},
function () {
    marqueePlay(); //Start the animation when mouse out
});

任何帮助,不胜感激。

这是我所能理解的。

问题:

  • 您想摆脱的默认操作简便性 慢启动,在动画中端快与慢的结局是不同的宽松 ,可应用于动画之一 在您的情况下,您显然不想要它,并且希望事物以非常线性的方式进行动画处理。
  • 没有直接的方法可以恢复我所知道的jQuery动画。 因此, 停止在悬停动画后,当你调用marqueePlay()再次,它会尝试在动画中定义的时间内剩余距离animateTime变量。 距离减少了,但是时间重新应用了。 因此,您的动画似乎会慢一些时间,然后恢复正常速度。 看起来只是这样,但实际上,它的行为完全符合其应有的行为。

建议的解决方案:

  • 根据上面的理解,可以使用requestAnimationFrame API轻松实现线性动画。
  • 避免使用jQuery的animate()方法,我们可以使用简单的布尔标志轻松地暂停恢复动画。

以下是正在使用的代码段,或者您是否有兴趣将其查看为jsFiddle

片段:

 // [http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/] window.requestAnimFrame=(function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(callback){window.setTimeout(callback,1000/60);};})(); var secDetails=$('#secDetails'); var container=secDetails.find('.container'); var eventsPartial=$('#events_partial'); var currTop=0; var destTop=eventsPartial.height()-secDetails.height()-parseInt(eventsPartial.css('margin-top')); var isPaused=false; var isFirstLoop=true; var speed=1; if(container.length>=4&&container.length<9){ requestAnimFrame(render); secDetails.hover(function(){isPaused=true;},function(){isPaused=false;}); currTop=destTop; } function render(){ requestAnimFrame(render); if(!isPaused){ secDetails.css({transform:'translate3d(1px,'+roundDecimal(currTop,2)+'px,0px)'}); currTop+=!isFirstLoop?-speed:speed; if(currTop>0) isFirstLoop=false; if(currTop<=destTop) currTop=0; } } function roundDecimal(value,place){ return Math.round(value*Math.pow(10,place))/Math.pow(10,place); } 
 #events_partial { min-height: 385px !important; margin-top: 57px; overflow: hidden; } .set-max-height { max-height: 385px !important; padding-top: 30px !important; } .sec-container { overflow: hidden !important; min-height: 200px; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div id="events_partial" class="container body-content col-md-12 col-lg-12 set-max-height"> <div class="row sec-container" id="secContainer"> <div id="secDetails"> <div class="container"> <h3><strong> Program in Place 1 &nbsp;on&nbsp;11/22/2015</strong></h3><p>Program Description which is a very long text on document and it can be more than 2 lines</p> </div> <div class="container"> <h3><strong> Program in Place 2 &nbsp;on&nbsp;11/22/2015</strong></h3><p>Program Description which is a very long text on document and it can be more than 2 lines</p> </div> <div class="container"> <h3><strong> Program in Place 3 &nbsp;on&nbsp;11/22/2015</strong></h3><p>Program Description which is a very long text on document and it can be more than 2 lines</p> </div> <div class="container"> <h3><strong> Program in Place 4 &nbsp;on&nbsp;11/22/2015</strong></h3><p>Program Description which is a very long text on document and it can be more than 2 lines</p> </div> <div class="container"> <h3><strong> Program in Place 5 &nbsp;on&nbsp;11/22/2015</strong></h3><p>Program Description which is a very long text on document and it can be more than 2 lines</p> </div> <div class="container"> <h3><strong> Program in Place 6 &nbsp;on&nbsp;11/22/2015</strong></h3><p>Program Description which is a very long text on document and it can be more than 2 lines</p> </div> <div class="container"> <h3><strong> Program in Place 7 &nbsp;on&nbsp;11/22/2015</strong></h3><p>Program Description which is a very long text on document and it can be more than 2 lines</p> </div> <div class="container"> <h3><strong> Program in Place 8 &nbsp;on&nbsp;11/22/2015</strong></h3><p>Program Description which is a very long text on document and it can be more than 2 lines</p> </div> </div> </div> </div> 

让我知道是否有任何不清楚的地方。

PS尽管个人而言,我不喜欢它到达最底端然后再次显示第一个并开始动画的突然跳动。 但我可以理解,这是您的方案中的一个要求,也许正是如此。 这是一种味道。 如果我按自己的方式去做的话,我会希望它们像动画一样上下移动。

更新:这是您的yoyo jsFiddle 还忘了提及您可以使用代码中的speed变量来控制动画的speed 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM