Skip to content

Instantly share code, notes, and snippets.

@Joseph-Reece
Last active January 12, 2022 06:55
Show Gist options
  • Save Joseph-Reece/3f13b474621427044deb97794be8e538 to your computer and use it in GitHub Desktop.
Save Joseph-Reece/3f13b474621427044deb97794be8e538 to your computer and use it in GitHub Desktop.
Smooth Scroll Jquery version Plus mobile Navbar close (bootstrap)
/* =========================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');
});
.navbar {
background-color: #ffffff !important;
opacity: 0.9;
color: #000000;
}
.navbar-expand-lg .navbar-nav {
padding: 10px 0;
}
/* center mobile navbar */
.navbar-expand-lg .navbar-nav .nav-item .nav-link {
padding: 10px 15px;
color: #060606;
text-align: center;
text-transform: capitalize;
/* text-transform: uppercase; */
}
.navbar-expand-lg .navbar-nav .nav-item .nav-link:hover {
color: #000000;
background-color: #AB4227;
border-radius: 5px;
}
@Joseph-Reece
Copy link
Author

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; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment