简体   繁体   English

如何缓慢地将DIV前后移动到背景上方?

[英]How to Move a DIV Back & Forth SLOWLY Overtop a Background?

Ok, so the full example I was going to show is no longer online so I will have to try to explain. 好的,所以我要显示的完整示例不再在线,因此我将不得不尝试进行解释。

What I'm trying to do is have a looping video as the full-screen background, and overtop of that will be a DIV containing a video that continuously moves SLOWLY back and forth across the screen. 我正在尝试做的是将循环视频作为全屏背景,而最上面的将是一个DIV,其中包含一个视频,该视频连续缓慢地在屏幕上来回移动。

Here's an example of the animation portion, yet it's too fast: 这是动画部分的示例,但是太快了:

fiddle sample 小提琴样本

HTML CODE HTML代码

<div style='position:absolute; background-color: blue; height: 50px; width:50px;'></div>

JS CODE JS代码

setInterval(function(){
    $("div").stop(true,true).animate({left: 300}, 10000, 
          function(){ $(this).stop(true,true).animate({left: 0}, 1000); 
    });
}, 20000);

I'm not very good with javascript so maybe someone can just help me figure out the timing aspect. 我对javascript不太满意,所以也许有人可以帮我弄清楚时序方面。

Thank you! 谢谢!

The second argument in the animate function is the duration of the animation, that determines how fast it will go. animate功能中的第二个参数是animate的持续时间,它确定动画进行的速度。 Right now it is 1000, which is 1000 milliseconds, or 1 second. 现在是1000,即1000毫秒,即1秒。 Make that number bigger to make it move more slowly. 增大该数字可使其移动得更慢。 However, the setInterval is going to kill you, it is going to stop it and call it again after 2 seconds, so that number needs to be changed to the total of both durations. 但是,setInterval将会杀死您,它将停止它,并在2秒后再次调用它,因此该数字需要更改为两个持续时间的总和。 However, that will be how long you will have to wait for the animation to begin. 但是,那将是您必须等待动画开始多长时间。 See how that is working here. 看看这里的运作方式。 If you want the animation to continue forever in a loop, you can do it this way: http://jsfiddle.net/bypepLf9/1/ 如果您希望动画永远循环下去,则可以这样进行: http : //jsfiddle.net/bypepLf9/1/

Keep in mind you are doing two separate animations, one to the right and another to the left, and they can call each other when they complete because the final argument is a callback that will be called when the animation is done. 请记住,您正在制作两个单独的动画,一个在右边,另一个在左边,并且它们可以在完成时相互调用,因为最后一个参数是在完成动画时将被调用的回调。

Here are the jQuery animate docs: http://api.jquery.com/animate/ but this should also be able to be accomplished through pure CSS. 这是jQuery的animate文档: http : //api.jquery.com/animate/,但这也应该能够通过纯CSS完成。

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

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