Topic: Updating smooth-scroll to support offset.

Robert McIntosh pro asked 7 years ago


Hello, thought I would drop another suggestion for your amazing work. I use the smooth-scroll option for my single page website and it has annoyed me that the fixed navbar always overlayed the content after scrolling to the content. I also didn't like the idea of adding a 60px margin to the top of each section. So I updated the smooth-scroll portion to allow you to define an offset. Here is the updated code below, use it or not is up to you.
//SMOOTH SCROLL
$(".smooth-scroll").on('click', 'a', function (event) {
    event.preventDefault();
    var elAttr = $(this).attr('href'),
    	offset = ($(this).data('offset') ? $(this).data('offset') : 0);
    $('body,html').animate({
        scrollTop: $(elAttr).offset().top + offset
    }, 700);
});


As always your post is really helpful! Thanks again for your support for MDB ;) We'll take a look at this ;)

Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Specification of the issue

  • ForumUser: Pro
  • Premium support: No
  • Technology: General Bootstrap questions
  • MDB Version: -
  • Device: -
  • Browser: -
  • OS: -
  • Provided sample code: Yes
  • Provided link: No
Tags