Skip to content

Instantly share code, notes, and snippets.

@germanescobar
Created January 2, 2019 13:41
Show Gist options
  • Save germanescobar/901616281198b678f7044166897846ed to your computer and use it in GitHub Desktop.
Save germanescobar/901616281198b678f7044166897846ed to your computer and use it in GitHub Desktop.

El contenido que va dentro de js/app.js es el siguiente:

$(document).ready(function () {
  $(document).on("scroll", onScroll);

  $('nav a[href^="#"]').on('click', function (e) {
    e.preventDefault();
    $(document).off("scroll");

    $('nav a').each(function () {
      $(this).removeClass('active');
    })
    $(this).addClass('active');

    var target = this.hash;
    $target = $(target);
    $('html, body').stop().animate({
      'scrollTop': $target.offset().top+2
    }, 500, 'swing', function () {
      window.location.hash = target;
      $(document).on("scroll", onScroll);
    });
  });
});

function onScroll(event){
  var scrollPosition = $(document).scrollTop();
  $('nav a').each(function () {
    var currentLink = $(this);
    var refElement = $(currentLink.attr("href"));
    if (refElement.position().top <= scrollPosition && refElement.position().top + refElement.height() > scrollPosition) {
      $('nav a').removeClass("active");
      currentLink.addClass("active");
    }
    else{
      currentLink.removeClass("active");
    }
  });
}

Para incluir jQuery debes copiar y pegar la siguiente línea:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment