Note that there are some explanatory texts on larger screens.

plurals
  1. POadd or remove class to div when scrolling page between anchors
    primarykey
    data
    text
    <p>I am developping a condition for my site, so that when i scroll my page to an anchor, the menu item corresponding to that anchor adds or removes a certain class.</p> <p>My problem is that i can't make the first item menu remove class, when scrolling to the second anchor, i think the problem may be in the condition, it may not be working the way i think so i need your help</p> <p>For this I am using jquery and here is what i've got so far:</p> <pre><code>jQuery(document).ready(function($) { var target1 = $("#thehotel").offset().top; var target2 = $("#thecity").offset().top; var interval = setInterval(function() { // i am not sure if this setInterval method is causing the problem, yet i didn't find any other solution if ($(window).scrollTop() &gt;= target1 &amp;&amp; $(window).scrollTop() &lt; target2) { $("#menu .item-109 a").addClass("myClass"); $("#menu .item-111 a").removeClass("myClass"); } else if ($(window).scrollTop() &gt;= target2) { $("#menu.item-109 a").removeClass("myClass"); $("#menu .item-111 a").addClass("myClass"); } }, 250); }); </code></pre> <p>I apologize if you find my english bad, it's not my mother language. Thank you.</p> <p><strong>I found my error, it is a typo!</strong></p> <p>here:</p> <pre><code> if ($(window).scrollTop() &gt;= target1 &amp;&amp; $(window).scrollTop() &lt; target2) { $("#menu .item-109 a").addClass("myClass"); $("#menu .item-111 a").removeClass("myClass"); } else if ($(window).scrollTop() &gt;= target2) { //change this $("#menu.item-109 a").removeClass("myClass"); //to this $("#menu .item-109 a").removeClass("myClass"); $("#menu .item-111 a").addClass("myClass"); } </code></pre> <p>The code is now working, sory for the trouble, Thank you all. Have a great day!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload