I have this code.
$('a').click(function(){
$('html, body').animate({
scrollTop: $( $.attr(this, 'href') ).offset().top
}, 500);
return false;
});
How can I make this script with a super smooth easing effect?
Save the href
attribute before calling animate
, otherwise the scope of this
is incorrect.
$(function() {
$('a').click(function() {
var href = $(this).attr("href");
$('html, body').animate({
scrollTop: $(href).offset().top
}, 500);
return false;
});
});
p {
height: 2000px;
}
#inner {
height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="#inner">Go to #inner</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div id="inner">I'm the #inner element.</div>