Last Updated: February 25, 2016
·
580
· chail93

ScrollingToElement

A simple and lightweight snippet to scrolling to an element. You must only add the class to anchor, use the ID as href and include this snippet with jQuery.

Code

$('.scrollto').on('click', function(event) {
    event.preventDefault();

    var $target = $(this).attr('href');
    $('html,body').animate({
      scrollTop: $($target).position().top
    }, 600, function() {
      window.location.hash = $target;
    });
  });
},

Example

<a href="#element" class="scrollto">Element 1</a>
<div id="element">
  <p>If you click on the anchor, then you scroll to this element here.</p>
</div>