Learn free online HTML5, javascript, and jquery Tutorials by Examples

Thursday, 29 September 2016

Smoothly scroll to an element without a jQuery plugin

                 

 

===========Scroll to a specific element===============

 

    $('html, body').animate({ 
           scrollTop: $("#target-element").offset().top 
    }, 1000);

 

               ===========Scroll to the selected anchor===============

$('a[href^="#"]').on('click', function(event) {
    var target = $(this.getAttribute('href'));
    if( target.length ) { 
         event.preventDefault();
     $('html, body').stop().animate({ 
       scrollTop: target.offset().top}, 
       1000); 
     }
});

 

No comments:

Post a Comment