I am using the following function to create a scroll animation to anchor links:
$('a').click(function(){
$('html, body').animate(
{scrollTop: $( $.attr(this, 'href') ).offset().top},
500 );
return false;
});
I would like to add easing. However, when I add 'easing' after '500' it breaks the script:
$('a').click(function(){
$('html, body').animate(
{scrollTop: $( $.attr(this, 'href') ).offset().top},
500, easing );
return false;
});
Any ideas what I am doing wrong?
First you need include jQuery.UI script then your code should look:
$('a').click(function(){
var top = $('body').find($(this).attr('href')).offset().top;
$('html, body').animate({
scrollTop: top
},500, 'easeOutExpo');
return false;
});
For your information:
Easing
The remaining parameter of .animate() is a string naming an easing function to use. An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing, and one that progresses at a constant pace, called linear. More easing functions are available with the use of plug-ins, most notably the jQuery UI suite.
this
which was in scope of animation method and
reference to body
and html
objects.easing
is not a method. Is a string type of animation property so you need
write it as string for example: 'easeOutExpo'
or "easeOutExpo"
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With