使用jquery编写完美动画的正确方法
Proper way to write a perfect animation using jquery
当前场景:
我有一个divs
的列表,它包含一组元素,这些元素根据存在的divs
的数量自动滚动。该divs
的列表被放置在parent
div
内并且具有固定的height
。
要求:
- 如果内部存在的
divs
的长度小于4,则不要设置动画,即滚动 - 以相同的速度制作动画,直到最后一个
div
,并从下向上滚动,反之亦然 - 在任何子
div
悬停时停止滚动
已实现:
- 滚动完成,即如果存在少于4个元素,则不滚动
- 悬停任何子
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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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
});
非常感谢您的帮助。
以下是我所能理解的内容。
问题:
- 有一个默认的ease,你想摆脱它。这个动画中的慢速开始、中速和慢速结束是可以应用于动画的不同缓和。在您的情况下,你显然不想要它,你会希望事物在非常线性时尚
- 据我所知,无法直接恢复jQuery动画。因此,在停止悬停时的动画后,当您再次调用
marqueePlay()
时,它将尝试在animateTime
变量中定义的时间内设置剩余距离的动画。距离会减少,但时间会重新应用。因此,您的动画似乎在一段时间内运行较慢,然后恢复到正常速度。它看起来只是这样,但事实上,它的行为完全符合它应该做的
建议的解决方案:
- 根据我以上的理解,您的线性动画可以非常使用CCD_ 13 API很容易实现
- 避免了jQuery的CCD_ 14方法,使我们可以轻松地暂停&使用简单的布尔标志恢复我们的动画
下面是正在运行的片段,或者如果您有兴趣将其作为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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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>
如果有什么不清楚的地方,请告诉我。
附言:尽管就我个人而言,我不喜欢当它到达最底部时发生的突然跳跃,然后再次显示第一个,并再次开始动画。但我可以理解,这是您场景中的一个要求,也许它正好适合这样。这是一种品味。如果我按自己的方式,我会让它们像溜溜球一样上下移动;)。
更新:
这是您的yoyo-jsFidle。还忘了提到可以使用代码中的speed
变量控制动画的速度。希望这能有所帮助。
相关文章:
- jquery:将动画绑定到滚动条位置的更好方法
- 如何抽象JQuery动画方法
- jQuery's的动画方法
- jQuery切换方法的动画不起作用
- 操作方法:第一次单击动画 1,第二次单击动画 2
- 使用each()方法来动画化代码类型
- 在要设置动画的所有方法之间进行选择
- 一种读取或更改CSS动画关键帧的方法
- 为D3.js动画编写绑定的正确方法是什么
- 如何设置“”的css值-webkit变换”;在“;动画化”;方法
- 这是在Javascript/Jquery中预加载图像的正确方法吗(带动画)
- 什么's是为网页编写动画标题的最有效方法
- 动画方法在 Chrome 中不起作用
- 使用 CSS3 或 JavaScript 对 Webkit 渐变进行动画处理的方法
- jquery:如何将 .hide() 方法与 .fadeToggle(“fast”) 动画一起使用
- 添加/删除类和动画元素的不同方法
- 将 css3 动画转换为画布动画的任何方法
- 取消对象方法内部的动画帧不起作用
- 操作方法:将鼠标悬停在图像上并制作动画绘制一条线,说明淡出
- 最流畅的改变jquery的方法.动画mid-animation