Smooth Scrolling to Any ID


/ Published in: jQuery
Save to your folder(s)

Simply set the id in the content, and use a hash in the anchor to scroll to it.


Copy this code and paste it in your HTML
  1. $(function($) {
  2. $(".scroll a, .top-btn").click(function(event) {
  3. event.preventDefault();
  4. $('html,body').animate({
  5. scrollTop: $(this.hash).offset().top
  6. }, 500);
  7. });
  8. });​
  9.  
  10. //usage
  11.  
  12. <a href="#theContent">Scroll to content</a>
  13.  
  14.  
  15. <div id="theContent">
  16. blah blah blah
  17. </div>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.