Skip to content

Instantly share code, notes, and snippets.

@IgnacioGaldames
Created April 22, 2020 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IgnacioGaldames/4c0e7452d4936c340e5ff360e99a98ad to your computer and use it in GitHub Desktop.
Save IgnacioGaldames/4c0e7452d4936c340e5ff360e99a98ad to your computer and use it in GitHub Desktop.
navegación con target on load y on click
$('header .navbar-nav a').on('click', function () {
$('header .navbar-nav').find('.active').removeClass('active');
$(this).parent('li').addClass('active');
});
$(window).on("load", function(){
$(function() {
setTimeout(function() {
if (location.hash) {
/* we need to scroll to the top of the window first, because the browser will always jump to the anchor first before JavaScript is ready, thanks Stack Overflow: http://stackoverflow.com/a/3659116 */
window.scrollTo(0, 0);
target = location.hash.split('#');
smoothScrollTo($('#'+target[1]));
}
}, 1);
// taken from: https://css-tricks.com/snippets/jquery/smooth-scrolling/
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
smoothScrollTo($(this.hash));
return false;
}
});
function smoothScrollTo(target) {
$('a[href^="#"]').click(function () {
var divId = $(this).attr('href');
$('html, body').animate({
scrollTop: $(divId).offset().top - 61
}, 100);
});
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top - 61
}, 100);
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment