Skip to content

Instantly share code, notes, and snippets.

@Kirill-Gorelov
Last active April 4, 2022 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Kirill-Gorelov/97c2fc105a8040a1b5100afa1e04f56a to your computer and use it in GitHub Desktop.
Save Kirill-Gorelov/97c2fc105a8040a1b5100afa1e04f56a to your computer and use it in GitHub Desktop.
/************ выделения пункта меню при активном якоре **************/
//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