I have side bullets that are menu scrolling to section of onepage, i did a scroll animation but i would like to change class="active"
between bullets when scrolling. I know it can be easly achieved by jQuery(body,window).scroll()
with $('#scrollspymenu li').each()
but its really bad way. I can break out of each()
function but this way i still need to get from first li element to (for example) 5th li. I thought about getting all data-target
elements to some array with save offsetTop
and offsetTop+height
and in scroll event i will check which element is on middle of the screen. ( i mean window scroll top position + 1/2 window height)
I have no idea how to make like some kind of timeline with sections with calculated range AND what function can select (based on input) element that is attached to 2 range values.
I'm also using scrollMagic, maybe it does it better? Still would like to use pure js or jquery for future use.
I'm sorry i couldnt form a good specific question but i tried my best.
my comment
(..) I want fastest possible way to get from scrolll event to proper element. Best possible performance of the page. It's stupid when u scroll, 1 scroll tick is 100x on scroll function, and every function goes every element. For 4 elements its checks elements 400 times...
JS
var page = $("html, body");
$('#scrollspymenu a, .go-to, .go-to2').click(function(event) {
event.preventDefault();
var target = $(this).attr('data-target');
if(target){
page.on("scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove", function(){
page.stop();
});
page.animate({ scrollTop: $(target).offset().top-50 }, 700, function(){
page.off("scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove");
});
}
})
Html Side menu
<nav id="scrollspymenu">
<ul>
<li class="active"><a href="#" data-target=".sec-home-knaufgroup"><span>Knauf Group</span></a></li>
<li><a href="#" data-target=".sec-company-history"><span>History</span></a></li>
<li><a href="#" data-target=".sec-company-values"><span>Values</span></a></li>
()...)
<li><a href="#" data-target=".sec-company-automobile"><span>Automobile industry</span></a></li>
<li><a href="#" data-target=".sec-company-solutions"><span>Solutions</span></a></li>
</ul>