我正在尝试制作一个平滑的javascript动画,使div在屏幕上从左900滑到550。
有两个变量: place和speed,我使用place来定位div在屏幕上的位置,速度来决定速度。
为了让它流畅,我试着让速度变得越来越慢,所以幻灯片开始得越来越快,变得越来越慢。
我不知道我是否在尝试做正确的事情,但基本上,我希望速度从100%开始,比如说50px,然后每一次都慢一点。
速度百分比应该等于我的位置的数量。
所以这个位置从900开始,到550结束。速度应该从50开始,每次都慢一个百分比,当位置在550结束时,速度在0结束。
我该如何设置?
我试过了:
function doit(place, speed, proc) {
var denne = document.getElementById("screen1");
if (place > 550) {
var speedproc = 100 - (place / 950 * 100); // (the reason that I'm using 950 here is because it should have a percentage to start from that isn't 0)
var newspeed = speed - (speed / 100 * proc);
speed = newspeed;
proc += speedproc;
place -= speed;
denne.style.marginLeft = place + "px";
setTimeout("doit(" + place + ", " + speed + ", " + proc + ")", 20);
}
}和身体:
<body onload='doit(900, 50, 0);'>但它表现得很疯狂。我做错了什么?
发布于 2013-12-30 04:50:20
总而言之,我会做这样的事情来创造减速效果:
newspeed = speed*proc; //Now, proc is a number between 0 and 1.
//the closer proc is to 1, the smaller the decrease will be.
// when proc is 1 - there will be no decrease in speed例如:
<body onload='doit(900, 50, 0.9);'>https://stackoverflow.com/questions/20829882
复制相似问题