【问题标题】:Proper way to write a perfect animation using jquery使用jquery编写完美动画的正确方法
【发布时间】:2015-06-25 18:08:22
【问题描述】:

当前情景:

我有一个divs 列表,其中包含一组元素,这些元素会根据存在的divs 的数量自动滚动。此divs 列表位于parent div 内,并带有固定的height

要求:

  • 如果内部存在的divs 的长度小于4,则不要动画,即滚动。
  • 以相同的速度制作动画直到最后一个 div 并从下往上滚动,反之亦然。
  • 在任何子 div 悬停时停止滚动。

达到:

  • 滚动已完成,即如果存在少于 4 个元素,则不会滚动。
  • 在任何子 div 悬停时滚动停止。
  • 动画直到底部元素并从头开始。

目前面临的问题:

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

DEMONSTRATION

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
});

非常感谢任何帮助。

【问题讨论】:

  • 似乎是easing 问题。 [Link] & [Link] 应该对我有帮助。

标签: javascript jquery jquery-animate requestanimationframe


【解决方案1】:

这是我能理解的。

问题:

  • 有一个默认的 ease 在起作用,您想摆脱它。这 动画中的慢开始、中快和慢结束是一个 可以应用于动画的不同 easing。在你的情况下, 您显然不想要它,并且您希望事物以动画形式呈现 非常线性时尚。
  • 据我所知,没有直接恢复 jQuery 动画的方法。因此,在 停止 悬停动画之后,当您再次调用 marqueePlay() 时,它将尝试在 animateTime 变量中定义的时间内为剩余距离设置动画。距离会减少,但时间会重新应用。因此,您的动画似乎会在一段时间内变慢,然后恢复正常速度。看起来是这样,但实际上,它的行为完全符合其应有的行为。

建议的解决方案:

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

下面是运行中的 sn-p,或者如果您有兴趣将其视为 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>

如果有什么不清楚的地方请告诉我。

附:虽然就个人而言,我不喜欢当它到达最底部然后再次显示第一个并再次开始动画时发生的突然跳跃。但我可以理解这是您的场景中的要求,也许它适合这种方式。这是一种味道。如果我按照自己的方式,我会让它们像悠悠球一样上下移动;)。

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

【讨论】:

  • 喜欢这个解决方案!!那是宾果!!如果可能的话,请您展示 像 yoyo 一样的动画 示例,如果我喜欢它,我会只使用它。.. :)
  • 在上面添加了一个更新
  • 非常感谢@Tahir。它工作正常,这真的很有帮助。再次感谢您的帮助! :)
  • 我希望我能给你的答案投票 10 次!!但不幸的是不可能......
  • 请你的朋友帮你解决这个问题;)
猜你喜欢
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
相关资源
最近更新 更多