Guest User

Untitled

a guest
Apr 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. var menu_selector = ".top-menu"; // Переменная должна содержать название класса или идентификатора, обертки нашего меню.
  2. function onScroll(){
  3. var scroll_top = $(document).scrollTop();
  4. $(menu_selector + " a").each(function(){
  5. var hash = $(this).attr("href");
  6. var target = $(hash);
  7. if (target.position().top <= scroll_top && target.position().top + target.outerHeight() > scroll_top) {
  8. $(menu_selector + " a.active").removeClass("active");
  9. $(this).addClass("active");
  10. } else {
  11. $(this).removeClass("active");
  12. }
  13. });
  14. }
  15. $(document).ready(function () {
  16. $(document).on("scroll", onScroll);
  17. $("a[href^=#]").click(function(e){
  18. e.preventDefault();
  19. $(document).off("scroll");
  20. $(menu_selector + " a.active").removeClass("active");
  21. $(this).addClass("active");
  22. var hash = $(this).attr("href");
  23. var target = $(hash);
  24. $("html, body").animate({
  25. scrollTop: target.offset().top
  26. }, 500, function(){
  27. window.location.hash = hash;
  28. $(document).on("scroll", onScroll);
  29. });
  30. });
  31. });
Add Comment
Please, Sign In to add comment