How to add smooth scrolling to Bootstrap's scroll spy function

前端 未结 8 1077
死守一世寂寞
死守一世寂寞 2020-11-30 17:30

I\'ve been trying to add a smooth scrolling function to my site for a while now but can\'t seem to get it to work.

Here is my HTML code relating to my navigation:<

8条回答
  •  感动是毒
    2020-11-30 17:54

    Do you really need that plugin? You can just animate the scrollTop property:

    $("#nav ul li a[href^='#']").on('click', function(e) {
    
       // prevent default anchor click behavior
       e.preventDefault();
    
       // store hash
       var hash = this.hash;
    
       // animate
       $('html, body').animate({
           scrollTop: $(hash).offset().top
         }, 300, function(){
    
           // when done, add hash to url
           // (default click behaviour)
           window.location.hash = hash;
         });
    
    });
    

    fiddle

提交回复
热议问题