Note that there are some explanatory texts on larger screens.

plurals
  1. POMoving static variables and related classes into a loop
    primarykey
    data
    text
    <p>I have some code in a Javascript file that was fixed at 3 elements.</p> <p>I am now pulling items from a database so I need to convert it into a loop to create the right amount of elements.</p> <p>I can't seem to figure out how to write my dynamic variables properly.</p> <p>This is what I originally had:</p> <pre><code>var block0=$('#block0').offset().top; var block1=$('#block1').offset().top; var block2=$('#block2').offset().top; $(window).scroll(function(){ windowScroll=$(window).scrollTop(); // conditional first item if(windowScroll &gt; block0){ $('.arrow').show(); $('.static_ele').hide(); $('#block_1').show(); $('#block_1').css({'position':'fixed','padding-top':0}); $('.arrow a').removeClass('upward'); }else{ $('.arrow a').attr('href','#block1'); } // all central items if(windowScroll &gt; block1-200){ $('.static_ele').hide(); $('#block_2').show(); $('#block_2').css('position','fixed'); $('.arrow a').attr('href','#block2'); $('.arrow a').removeClass('upward'); } // conditional last item if(windowScroll &gt; block2-200){ $('.static_ele').hide(); $('#block_3').show(); $('#block_3').css('position','fixed'); $('.arrow a').attr('href','#arrow1'); $('.arrow a').addClass('upward'); } }) </code></pre> <p>This is where I have now got to:</p> <pre><code>var i=1; var len={{collection.products_count}}; $(window).scroll(function(){ windowScroll=$(window).scrollTop(); // first block if(windowScroll &gt; $('#block0').offset().top){ $('.arrow').show(); $('.static_ele').hide(); $('#block_1').show(); $('#block_1').css({'position':'fixed','padding-top':0}); $('.arrow a').removeClass('upward'); }else{ $('.arrow a').attr('href','#block1'); } // central blocks for (i; i&lt;len-2; i++) { if(windowScroll &gt; $('#block'+i).offset().top-200){ $('.static_ele').hide(); $('#block_'+(i+1)).show(); $('#block_'+(i+1)).css('position','fixed'); $('.arrow a').attr('href','#block'+(i+1)); $('.arrow a').removeClass('upward'); } } // end for loop // last block if(windowScroll &gt; $('#block'+(len-1)).offset().top-200){ $('.static_ele').hide(); $('#block_'+len).show(); $('#block_'+len).css('position','fixed'); $('.arrow a').attr('href','#arrow1'); $('.arrow a').addClass('upward'); } }) // end window scroll function </code></pre> <p>Which still is not working. If I take the items out of the four loop and manually enter them it works. So if I replace the For loop above with</p> <pre><code>// central blocks if(windowScroll &gt; $('#block1').offset().top-200){ $('.static_ele').hide(); $('#block_2').show(); $('#block_2').css('position','fixed'); $('.arrow a').attr('href','#block2'); $('.arrow a').removeClass('upward'); } if(windowScroll &gt; $('#block2').offset().top-200){ $('.static_ele').hide(); $('#block_3').show(); $('#block_3').css('position','fixed'); $('.arrow a').attr('href','#block3'); $('.arrow a').removeClass('upward'); } </code></pre> <p>It then works perfectly for a scenario with 4 items. So I think it is because all cases need to be accessible from within the windowScroll function which they aren't when contained within a for loop?? Is there another way to code this?</p> <p>(A bit of background - this script is to hide and show divs as a user scrolls down a page. An arrow at the bottom of the page is available to click to next slide. When you reach the bottom the arrow turns up and takes you back to the top.) </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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