Created
February 17, 2017 10:40
-
-
Save fiximportant/3f2d06dc9442b8ed302bfe31a82e2469 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
$(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