jQuery 沿着贝塞尔曲线制作动画,每个周期出现不规则的 y 轴偏移
我基本上是在正弦波上为对象设置动画。然而,它只遍历一个段一次,而我需要它不断地遍历。
我递归地重复动画以使其遵循路径,而不是通常从头开始重新启动的方式。
然而,循环的每次重复都会在下一个循环开始时将元素稍微降低。
我怎样才能让它无缝地遵循曲线?
查看演示:http://jsfiddle.net/nfeZF/121/
function float(dir){
var x_current = $("div").offset().left;
var y_current = $("div").offset().top;
var SineWave = function() {
// this is called every step in the animation over 6000 miliseconds, updating the x/y coordinates
// with sin rules applied to follow the curve
// repeating this cycle (each timeout callback) drops the
// object about 50-100 pixels down from where it left off
this.css = function(p) {
var s = Math.sin(p*10);
if (!dir){
// swap the direction to continue on the path
s = -s;
}
var x = 300-p * 300;
var y = s * 100 + 100;
var o = ((s+2)/4+0.1);
// add the current x-position to continue the path, rather than restarting
return {top: y + "px", left: x_current + x + "px"};
}
};
$("div").animate({
path: new SineWave
}, 6000, 'linear', function(){
// avoid exceeding stack
setTimeout(float(!dir), 0);
});
}
I'm basically animating an object over a sin wave. However, it only traverses a segment once, whereas I need it to continually traverse.
I am repeating the animation recursively for it to follow the path, rather than how it would normally restart back at the beginning.
However, each repeat of the cycle drops the element slightly lower at the beginning of the next cycle.
How can I make this follow the curve seamlessly?
View Demonstration:http://jsfiddle.net/nfeZF/121/
function float(dir){
var x_current = $("div").offset().left;
var y_current = $("div").offset().top;
var SineWave = function() {
// this is called every step in the animation over 6000 miliseconds, updating the x/y coordinates
// with sin rules applied to follow the curve
// repeating this cycle (each timeout callback) drops the
// object about 50-100 pixels down from where it left off
this.css = function(p) {
var s = Math.sin(p*10);
if (!dir){
// swap the direction to continue on the path
s = -s;
}
var x = 300-p * 300;
var y = s * 100 + 100;
var o = ((s+2)/4+0.1);
// add the current x-position to continue the path, rather than restarting
return {top: y + "px", left: x_current + x + "px"};
}
};
$("div").animate({
path: new SineWave
}, 6000, 'linear', function(){
// avoid exceeding stack
setTimeout(float(!dir), 0);
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论