jQuery从左到右动画定位问题
我试图将 div 从屏幕的一端滑动到另一端,但我试图使其循环,这样一旦它到达右侧,它将再次从左侧开始,依此类推。 ..
我现在遇到的问题是,一旦元素到达右侧,它就会循环,但该元素只是在左上角弹出,因为我希望它从浏览器窗口的外部移入,以给出它的无缝效果,这是我的代码,
Javascript/jQuery:
$(document).ready(function(){
var body_width;
var timer;
jQuery(function($) {
timer = setTimeout(Cloud, 0);
});
function Cloud() {
//get the width of the body
body_width = $("body").width();
$("#move_me").css({margin:0}).
animate({marginLeft: body_width}, body_width*5, "linear", function() {
timer = setTimeout(Cloud, 0);
});
}
});
HTML:
<div id="header">
<div id="move_me"></div>
</div>
CSS:
#header {
width: 100%;
height: 250px;
background: #000;
overflow: hidden;
}
#move_me {
height: 250px;
width: 250px;
background: #F00;
}
提前谢谢!
I am trying to slide a div from one end of the screen to the next, but I am trying to make it loop, so that once it has reached the right side, it will start again from the left and so on and so forth...
The problem I have now is that once the element reaches the right side, it loops, but the element just pops up in the left hand corner, where as I would like it to move in from outside of the browser window, to give it that seamless effect, here is my code,
Javascript/jQuery:
$(document).ready(function(){
var body_width;
var timer;
jQuery(function($) {
timer = setTimeout(Cloud, 0);
});
function Cloud() {
//get the width of the body
body_width = $("body").width();
$("#move_me").css({margin:0}).
animate({marginLeft: body_width}, body_width*5, "linear", function() {
timer = setTimeout(Cloud, 0);
});
}
});
HTML:
<div id="header">
<div id="move_me"></div>
</div>
CSS:
#header {
width: 100%;
height: 250px;
background: #000;
overflow: hidden;
}
#move_me {
height: 250px;
width: 250px;
background: #F00;
}
Thanx in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在元素离开屏幕的情况下启动动画即可。
因此,您需要将其左边距设置为等于元素的负宽度。
演示: http://jsfiddle.net/gaby/XKVtC/
Just start the animation with the element out of the screen.
So you need to set its left margin equal to the negative width of the element..
demo: http://jsfiddle.net/gaby/XKVtC/