javascript - Smooth scrolling not so smooth -
i have been trying create smooth scrolling plain js. tried typical animation timing using this:
function animate(x) { var start = new date(); var id = setinterval(function () { var timepassed = new date() - start; var progress = timepassed / x.duration; if (progress > 1) { progress = 1; } var delta = x.delta(progress); x.step(delta); if (progress == 1) { clearinterval(id); } }, x.delay); }
after i'm measuring offsettop
elements , using scrollto()
there using window.scrollto(0, delta * to.offsettop);
delta
returned progress of time. problem not seem smooth eyes. missing important information animation?
(using else if find out way anmiation should go - @ least now)
there 2 aspects issue here:
- performance
- perceived smoothness
performance
in code gain performance using requestanimationframe()
instead of setinterval()
. there nice article transitioning new method on mozilla or in-depth article paul irish. note: because want achieve 60fps, code shouldn't take longer 16ms calculation time.
perceived animation smoothness & easing
for second issue: animation linear, it's boring. when (if ever did) use animation-library or jquery animation, can specify timing-function animation should use. these example 'ease-in', 'ease-out', , on. can implement these in function through math. there many tools in web visualize these easing-equotations in different ways.
if hungry more input, check out robert penner's easing page.
last, not least, take @ this gist, provides javascript-implementation of different easing functions - oh , possible solution in vanilla js ;)
p.s. demonstration, your code little ease
Comments
Post a Comment