javascript - How to to addClass() to the active link of two different navs based on scroll position in jQuery? -
i have 2 navs, both class of .fixed-nav
, add class active link of each nav (i.e. .menu__item--current
#main-nav
, .is-selected
#cd-vertical-nav
), based on scroll position (snippet below).
this part seems wrong:
updatemainnavigation(), updateverticalnavigation(); $(window).on('scroll', function() { updatemainnavigation(), updateverticalnavigation(); });
my first try doesn't work:
updatemainnavigation(); $(window).on('scroll', function() { updatemainnavigation(); }); updateverticalnavigation() $(window).on('scroll', function() { updateverticalnavigation(); });
you can add function listen scrolling. calculate div @ , select element , change css. can tweak number
calculation if want change earlier. add class vertical-navs
. in case, class="vert-nav"
.
function changedots(distancefromtop) { var number = math.ceil(distancefromtop / 400); $('.cd-dot').css({background: "#fff"}); $('.is-selected').removeclass("is-selected"); if (distancefromtop === 0) { $($('.cd-dot')[0]).css({background: "#000"}); $($('.vert-nav')[0]).addclass("is-selected"); } else { $($('.cd-dot')[number - 1]).css({background: "#000"}) $($('.vert-nav')[number - 1]).addclass("is-selected"); } } $(window).scroll(function() { var distancefromtop = $(this).scrolltop(); changedots(distancefromtop); });
here's example: codepen
Comments
Post a Comment