Skip to content

Instantly share code, notes, and snippets.

@fiximportant
Created February 17, 2017 10:40
Show Gist options
  • Save fiximportant/3f2d06dc9442b8ed302bfe31a82e2469 to your computer and use it in GitHub Desktop.
Save fiximportant/3f2d06dc9442b8ed302bfe31a82e2469 to your computer and use it in GitHub Desktop.
Плавающий блок в пределах родителя
$(document).ready(function(){
var fatherWrap = $('.column__center'), //Родитель
childBlock = $('.js-left-menu'), //Ребенок который ездит в пределах родителя
startPosition = fatherWrap.offset().top, //Позиция с которой начинается скролл ребенка
stopPosition = $('.js-left-menu-stop').offset().top - childBlock.outerHeight(); //DIV на котором, нужно остановиться
$(document).scroll(function () {
var y = $(this).scrollTop()
if (y > startPosition) {
childBlock.addClass('b-menu-left-fixed'); //Добавляем класс position - fixed
if (y > stopPosition) {
childBlock.css('top', stopPosition - y);
} else {
childBlock.css('top', 0);
}
} else {
childBlock.removeClass('b-menu-left-fixed'); //Удаляем класс
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment