Last active
June 4, 2018 12:43
-
-
Save zdimaz/5efae6ca6059c90cec4ca2c2bac11f42 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
<ul class="navigation clearfix"> | |
<li> | |
<a href="#block1" class="navigation_item current">Scroll block 1</a> | |
</li> | |
<li> | |
<a href="#block2" class="navigation_item">Scroll block 2</a> | |
</li> | |
<li> | |
<a href="#block3" class="navigation_item">Scroll block 3</a> | |
</li> | |
<li> | |
<a href="#block4" class="navigation_item">Scroll block 4</a> | |
</li> | |
</ul> | |
<div id="block1" class="section">Scroll block 1</div> | |
<div id="block2" class="section">Scroll block 2</div> | |
<div id="block3" class="section">Scroll block 3</div> | |
<div id="block4" class="section">Scroll block 4</div> | |
self.navigation.init(); | |
/** | |
** Main navigation | |
**/ | |
navigation: { | |
init: function () { | |
var self = this; | |
self.w = $(window); | |
self.body = $('body'); | |
self.nav = $('.wrapp_header.pos_absolute'); | |
self.section = $('.section'); | |
self.sectionQt = self.section.length; | |
self.anchorScroll(); | |
self.openCloseMenu(); | |
self.w.on('scroll',function(){ | |
self.pageScroll(); | |
self.sticky(); | |
}); | |
// sticky | |
self.header = $('.wrapp_header.pos_absolute'); | |
self.sticky(); | |
self.w.on('resize',function(){ | |
self.sticky(); | |
}); | |
}, | |
anchorScroll: function(){ | |
var self = this; | |
self.nav.on('click', ".navigation_item", function(event){ | |
event.preventDefault(); | |
var $this = $(this), | |
item = $this.parent(), | |
dataId = $this.attr('href'), | |
offset = $(dataId).offset().top; | |
$this.addClass('current').parent().siblings().find('a').removeClass('current'); | |
self.scrollContent(offset); | |
}); | |
}, | |
scrollContent: function(offset){ | |
var self = this; | |
self.body.addClass('scrollContent'); | |
$('html,body').stop().animate({ | |
scrollTop: offset | |
},1200,function(){ | |
self.body.removeClass('scrollContent'); | |
}); | |
}, | |
pageScroll: function(){ | |
var self = this; | |
if(self.body.hasClass('scrollContent'))return; | |
self.wScroll = self.w.scrollTop(); | |
self.wHeightHalf = self.w.height()/2; | |
for (var i = 0; i <= self.sectionQt - 1; i++) { | |
var offset = $(self.section[i]).offset().top, | |
heightBox = $(self.section[i]).outerHeight(), | |
bottomOffset = $(self.section[i+1]).length ? $(self.section[i+1]).offset().top : offset + heightBox, | |
id = $(self.section[i]).attr('id'), | |
activItem = $('.wrapp_header.pos_absolute').find("a[href='" + "#" + id + "']"); | |
if(self.wScroll + self.wHeightHalf > offset && self.wScroll + self.wHeightHalf < bottomOffset ){ | |
activItem.addClass('current').parent().siblings().find('a').removeClass('current'); | |
return false; | |
} | |
}; | |
}, | |
sticky: function(){ | |
var self = this; | |
self.wHeight = self.w.height(); | |
self.wScroll = self.w.scrollTop(); | |
self.headerHeight = self.header.outerHeight(); | |
if(self.wScroll <= self.headerHeight){ | |
if(self.position == 'top') return false; | |
self.position = 'top'; | |
self.header.removeClass('sticky').stop().animate({ | |
"top": 0 | |
},0); | |
} | |
else if(self.wScroll > self.headerHeight && self.wScroll < self.wHeight/1.5){ | |
if(self.position == 'hide') return false; | |
if(self.position == 'top'){ | |
self.position = 'hide'; | |
self.header.addClass('sticky').stop().animate({ | |
'top': -self.headerHeight | |
},0); | |
} | |
else{ | |
self.position = 'hide'; | |
self.header.stop().animate({ | |
'top': -self.headerHeight | |
},300, function(){ | |
self.header.addClass('sticky'); | |
}); | |
} | |
} | |
else{ | |
if(self.position == 'sticky') return false; | |
self.position = 'sticky'; | |
self.header.addClass('sticky'); | |
self.header.stop().animate({ | |
'top': 0 | |
}); | |
} | |
}, | |
openCloseMenu: function() { | |
$(".nav_btn").on("click", function () { | |
$(this).toggleClass("active").stop(); | |
$(".navigation").toggleClass("open").stop(); | |
}); | |
}, | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment