Answer the question
In order to leave comments, you need to log in
How to improve/smooth the backgroundPosition animation in JavaScript?
Hello, tell me please.
There is an EaseOut animation that scrolls the background a certain distance.
If the distance is not large, then the effect works fine, but if I set the animation to scroll by about >= 300px, then the animation turns out to be some kind of “torn” ... And another question, when the distance is not large, then the effect is slow, but if >= 300px, then the animation tries to cover the distance in the same time, but naturally at a higher speed.
Please help me to make sure that regardless of the distance, the speed of the animation is the same.
Here is the code:
function set_animate(opts) {
var start = new Date,
timer = setInterval(function () {
var progress = (new Date - start) / opts.duration;
if (progress > 1) {
progress = 1;
}
opts.step(opts.delta(progress));
if (progress == 1) {
clearInterval(timer);
}
}, 34);
}
function get_animate(elem, delta, duration) {
var to = 58;
set_animate({
delay: 10,
duration: duration || 1000,
delta: delta,
step: function (delta) {
$(elem).css('backgroundPosition', '0 -' + to * delta + 'px');
}
});
}
function bounce(progress) {
for (var a = 0, b = 1, result; 1; a += b, b /= 2) {
if (progress >= (7 - 4 * a) / 11) {
return -Math.pow((11 - 6 * a - 11 * progress) / 4, 2) + Math.pow(b, 2);
}
}
}
function makeEaseOut(delta) {
return function (progress) {
return 1 - delta(1 - progress);
}
}
get_animate(slide.number_1, makeEaseOut(bounce));
Answer the question
In order to leave comments, you need to log in
Try using css transition, it should work smoother.
Here is an example jsfiddle.net/hfXSs/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question