Je dois exécuter la méthode javascript scrollTo (x, y) avec animation. Je ne peux pas utiliser jQuery pour le faire.Défilement avec animation
5
A
Répondre
2
Vous pouvez animer (presque) n'importe quoi dans Javascript en utilisant les appels window.setTimeout() et window.clearTimeout(). Voir http://www.w3schools.com/js/js_timing.asp
9
function interpolate(source,target,pos) {
return (source + (target - source) * pos);
}
function easing(pos) {
return (-Math.cos(pos * Math.PI)/2) + 0.5;
}
function scrollTop(duration) {
duration = duration || 1000;
var startY = window.pageYOffset,
start = Number(new Date()),
finish = start + duration;
var interval = setInterval(function() {
var now = Number(new Date()),
pos = (now > finish) ? 1 : (now - start)/duration;
scrollTo(0, interpolate(startY, 0, easing(pos)));
if (now > finish)
clearInterval(interval);
}, 15);
};