Search code examples
javascriptjquerycsssmooth-scrolling

Smooth scroll for multiple fixed navbar with different height


I have a fixed-top navbar (75px height) with smooth scroll working perfectly well for desktop. When I'm on small screen I have a different navbar (50px height) with less height so the anchor dosent arrive a the right place.

// Smooth Scoll
$('a[href*="#"]')
    .not('[href="#"]')
    .not('[href="#0"]')
.click(function(event) {
       if (
        location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//,'')
        &&
            location.hostname == this.hostname
       ) {
           var target = $(this.hash);
           target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
           if (target.length) {
               event.preventDefault();
               $('html, body').animate({
                   scrollTop: target.offset().top -75
               }, 1200, function() {
                   var $target = $(target);
                   $target.focus();
                   if ($target.is(":focus")) {
                       return false;
                   } else {
                       $target.attr('tabindex','-1');
                       $target.focus();
                   };
               });
           }
       }
});

I want to be able to set the target.offset().top depending on the navbar that is clicked. No css solutions please.


Solution

  • Add a screen width check and change the 75 to 50

    // Smooth Scoll
    $('a[href*="#"]').not('[href="#"]') .not('[href="#0"]').click(function(event) {
           if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
               var target = $(this.hash);
               target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
               if (target.length) {
                   event.preventDefault();
                   $('html, body').animate({
    
                    // change dependant on screen width
                    if ($(window).width() < 960) { // set in px
                        scrollTop: target.offset().top -50
                    } else {
                       scrollTop: target.offset().top -75
                    }          
    
                   }, 1200, function() {
                       var $target = $(target);
                       $target.focus();
                       if ($target.is(":focus")) {
                           return false;
                       } else {
                           $target.attr('tabindex','-1');
                           $target.focus();
                       };
                   });
               }
           }
    });
    

    Or you could, which might be better get the nav height on click

    //set nav 
    var nav = $('.nav-bar'); // update to your nav class
    
    // Smooth Scoll
    $('a[href*="#"]').not('[href="#"]').not('[href="#0"]').click(function(event) {
    
        // get nav height
        var NavHeight = nav.height();
    
           if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname ) {
               var target = $(this.hash);
               target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
               if (target.length) {
                   event.preventDefault();
                   $('html, body').animate({
    
                    // minus nav height
                    scrollTop: target.offset().top - NavHeight
    
                   }, 1200, function() {
                       var $target = $(target);
                       $target.focus();
                       if ($target.is(":focus")) {
                           return false;
                       } else {
                           $target.attr('tabindex','-1');
                           $target.focus();
                       };
                   });
               }
           }
    });