Last active
January 12, 2022 06:55
-
-
Save Joseph-Reece/3f13b474621427044deb97794be8e538 to your computer and use it in GitHub Desktop.
Smooth Scroll Jquery version Plus mobile Navbar close (bootstrap)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* =========================Smooth single page Navigaton======================== */ | |
// Select all links with hashes | |
$('a[href*="#"]') | |
// Remove links that don't actually link to anything | |
.not('[href="#"]') | |
.not('[href="#0"]') | |
.click(function(event) { | |
// On-page links | |
if ( | |
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') | |
&& | |
location.hostname == this.hostname | |
) { | |
// Figure out element to scroll to | |
var target = $(this.hash); | |
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); | |
// Does a scroll target exist? | |
if (target.length) { | |
// Only prevent default if animation is actually gonna happen | |
event.preventDefault(); | |
$('html, body').animate({ | |
scrollTop: target.offset().top | |
}, 1000, function() { | |
// Callback after animation | |
// Must change focus! | |
var $target = $(target); | |
$target.focus(); | |
if ($target.is(":focus")) { // Checking if the target was focused | |
return false; | |
} else { | |
$target.attr('tabindex','-1'); // Adding tabindex for elements not focusable | |
$target.focus(); // Set focus again | |
}; | |
}); | |
} | |
} | |
}); | |
/* Scroll to top */ | |
$("a[href='#top']").click(function() { | |
$("html, body").animate({ scrollTop: 0 }, "slow"); | |
return false; | |
}); | |
/*==================close navbar on click================*/ | |
$('.navbar-nav>li>a').on('click', function(){ | |
$('.navbar-collapse').collapse('hide'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CSS To center Bootstrap Mobile Navbar
/* center mobile navbar */ .navbar-expand-lg .navbar-nav .nav-item .nav-link { padding: 10px 15px; color: #060606; text-align: center; text-transform: uppercase; }