Skip to content

Instantly share code, notes, and snippets.

@Jekins
Created March 2, 2016 15:02
Show Gist options
  • Save Jekins/efc36d64e22a60e14682 to your computer and use it in GitHub Desktop.
Save Jekins/efc36d64e22a60e14682 to your computer and use it in GitHub Desktop.
Навешивание класса при скролле до элемента
var ScrollSpy = (function () {
var
screen = '[data-element="screen"]',
list = '[data-toggle="scroll-spy"]',
section = '[data-element="section"]',
active = 'active',
offset = $(window).height() / 1.4;
return {
init: function () {
this.scroll();
},
scroll: function (){
var
listItems = $(list).find(section),
scrollItems = listItems.map(function () {
var item = $($(this));
if (item.length) {
return item;
}
});
$(screen).scroll(function () {
scrollItems.map(function () {
if ($(this).offset().top < offset) {
$(this).addClass(active);
}
});
});
}
};
})();
$(function () {
ScrollSpy.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment