Change class on Scroll - Vertical Dot
Hi there,
Im trying to write the cleanest code to change a dot colour on a vertical dot navigation element. Basically, when the user scrolls to the next section, the class should be removed on the old circle, and added to the new one.
This is what happens when the user clicks the button
(the code is wrapped inside a click function).
- //Change dot class
- circle_curr = '#' + curr_pos.toLowerCase() + '_circle';
- circle_target = target + '_circle';
- $(circle_curr).removeClass("current");
- $(circle_target).addClass("current");
However I would like to update the classes on scroll as well. I have tried using the link here, but cant relate it to my code.
Heres what I have so far:
- $(window).on('scroll', function(e) {
- var elems = $('#home, #about, #crafts, #contact'),
- scrolled = $(this).scrollTop();
-
- var dataPage = elems.filter(function() {
- return $(this).offset().top + ($(this).height() / 2) >= scrolled;
- }).first();
- console.log(dataPage);
- var section = '#' + dataPage.attr('id') + "_circle";
- console.log(section);
- $(section).addClass("current");
- $(section).removeClass("current");
- }).trigger('scroll');
And heres my HTML:
<nav class="hidden">
<ul>
<li>
<a href="#home">
<div class="current circle" id="home_circle">
</div>
<span>
Home
</span>
</a>
</li>
<li>
<a href="#about">
<div class="circle" id="about_circle">
</div>
<span>
About
</span>
</a>
</li>
<li>
<a href="#crafts">
<div class="circle" id="crafts_circle">
</div>
<span>
Crafts
</span>
</a>
</li>
<li>
<a href="#contact">
<div class="circle" id="contact_circle">
</div>
<span>
Contact
</span>
</a>
</li>
</ul>
</nav>
Many thanks in advance.