我从Nina Scholz's answer从左到右完全理解这种逻辑。但是,如何使用负值进行逆运算呢?
假设开始是正确的,结束是0。但是我们仍然要编辑el.style.left
属性。
这就是我进行调整时发生的事情-就像是https://jsfiddle.net/wfcoaxs0/1/闪烁
如果我仅更改开始/结束位置,那就行不通了
// formula http://easings.net/
// description https://stackoverflow.com/questions/8316882/what-is-an-easing-function
// x: percent
// t: current time,
// b: beginning value,
// c: change in value,
// d: duration
function easeInOutQuad(x, t, b, c, d) {
if ((t /= d / 2) < 1) {
return c / 2 * t * t + b;
} else {
return -c / 2 * ((--t) * (t - 2) - 1) + b;
}
}
function move() {
//position += increment;
time += 1 / fps;
position = easeInOutQuad(time * 100 / duration, time, start, finish, duration);
if (position >= finish) {
clearInterval(handler);
box.style.left = finish + "px";
return;
}
box.style.left = position + "px";
}
var box = document.getElementById("box"),
fps = 60,
duration = 2, // seconds
start = window.innerWidth - box.clientWidth, // pixel
finish = 0,
distance = finish - start,
increment = distance / (duration * fps),
position = start,
time = 0,
handler = setInterval(move, 1000 / fps);
body {
background: gainsboro;
}
#box {
width: 100px;
height: 100px;
background: white;
box-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
position: absolute;
//left: 0;
}
<div id="box"></div>