Last active
April 4, 2022 09:37
-
-
Save Kirill-Gorelov/97c2fc105a8040a1b5100afa1e04f56a to your computer and use it in GitHub Desktop.
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
/************ выделения пункта меню при активном якоре **************/ | |
//http://jsfiddle.net/bonilka/p7sgwg4L/ | |
<script> | |
jQuery(window).scroll(function(){ | |
var $sections = $('div'); | |
$sections.each(function(i,el){ | |
var top = $(el).offset().top-100; | |
var bottom = top +$(el).height(); | |
var scroll = $(window).scrollTop(); | |
var id = $(el).attr('id'); | |
if( scroll > top && scroll < bottom){ | |
if (id != undefined) { | |
// $('sidebar ul li a').removeClass('active'); | |
$('a.active').removeClass('active'); | |
console.log(id+'id'); | |
// $('a[href="#'+id+'"]').removeClass('active'); | |
$('a[href="#'+id+'"]').addClass('active'); | |
} | |
} | |
}) | |
}); | |
$("sidebar ul li a").on("click","a", function (event) { | |
// исключаем стандартную реакцию браузера | |
event.preventDefault(); | |
// получем идентификатор блока из атрибута href | |
var id = $(this).attr('href'), | |
// находим высоту, на которой расположен блок | |
top = $(id).offset().top; | |
// анимируем переход к блоку, время: 800 мс | |
// $('body,html').animate({scrollTop: top}, 800); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment