I've got a piece of code that takes an element, checks where it is, and if it's beyond a set place in the viewport, make the opacity increase to 1. I've made the code so that it only runs the checks if they're needed, the problem is that the code looks atrocious, and I suspect there's a better way for me to be doing it.
var $wheresThisAt = viewportHeight*4;
if (scrolled > ($wheresThisAt))
{
//there has to bve a better way...
$('.salafoot').css('opacity', '0');
$('#salamander-1').css('opacity', '1');
if (scrolled > (($wheresThisAt)+20)){
$('#salamander-2').css('opacity', '1');
if (scrolled > (($wheresThisAt)+40)){
$('#salamander-3').css('opacity', '1');
if (scrolled > (($wheresThisAt)+60)){
$('#salamander-4').css('opacity', '1');
if (scrolled > (($wheresThisAt)+80)){
$('#salamander-5').css('opacity', '1');
if (scrolled > (($wheresThisAt)+100)){
$('#salamander-6').css('opacity', '1');
if (scrolled > (($wheresThisAt)+120)){
$('#salamander-7').css('opacity', '1');
if (scrolled > (($wheresThisAt)+140)){
$('#salamander-8').css('opacity', '1');
if (scrolled > (($wheresThisAt)+160)){
$('#salamander-9').css('opacity', '1');
if (scrolled > (($wheresThisAt)+180)){
$('#salamander-10').css('opacity', '1');
if (scrolled > (($wheresThisAt)+200)){
$('#salamander-11').css('opacity', '1');
if (scrolled > (($wheresThisAt)+220)){
$('#salamander-12').css('opacity', '1');
if (scrolled > (($wheresThisAt)+240)){
$('#salamander-13').css('opacity', '1');
if (scrolled > (($wheresThisAt)+260)){
$('#salamander-14').css('opacity', '1');
if (scrolled > (($wheresThisAt)+280)){
$('#salamander-15').css('opacity', '1');
if (scrolled > (($wheresThisAt)+300)){
$('#salamander-16').css('opacity', '1');
if (scrolled > (($wheresThisAt)+320)){
$('#salamander-17').css('opacity', '1');
if (scrolled > (($wheresThisAt)+340)){
$('#salamander-18').css('opacity', '1');
if (scrolled > (($wheresThisAt)+360)){
$('#salamander-19').css('opacity', '1');
if (scrolled > (($wheresThisAt)+380)){
$('#salamander-20').css('opacity', '1');
if (scrolled > (($wheresThisAt)+400)){
$('#salamander-21').css('opacity', '1');
if (scrolled > (($wheresThisAt)+420)){
$('#salamander-22').css('opacity', '1');
if (scrolled > (($wheresThisAt)+440)){
$('#salamander-23').css('opacity', '1');
if (scrolled > (($wheresThisAt)+460)){
$('#salamander-24').css('opacity', '1');
if (scrolled > (($wheresThisAt)+480)){
$('#salamander-25').css('opacity', '1');
if (scrolled > (($wheresThisAt)+500)){
$('#salamander-26').css('opacity', '1');
if (scrolled > (($wheresThisAt)+520)){
$('#salamander-27').css('opacity', '1');
if (scrolled > (($wheresThisAt)+540)){
$('#salamander-28').css('opacity', '1');
if (scrolled > (($wheresThisAt)+560)){
$('#salamander-29').css('opacity', '1');
if (scrolled > (($wheresThisAt)+580)){
$('#salamander-30').css('opacity', '1');
if (scrolled > (($wheresThisAt)+600)){
$('#salamander-31').css('opacity', '1');
if (scrolled > (($wheresThisAt)+620)){
$('#salamander-32').css('opacity', '1');
if (scrolled > (($wheresThisAt)+640)){
$('#salamander-33').css('opacity', '1');
if (scrolled > (($wheresThisAt)+660)){
$('#salamander-34').css('opacity', '1');
if (scrolled > (($wheresThisAt)+680)){
$('#salamander-35').css('opacity', '1');
if (scrolled > (($wheresThisAt)+700)){
$('#salamander-36').css('opacity', '1');
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} // close all if statements before if (scrolled > ($wheresThisAt))
} // end if (scrolled > ($wheresThisAt))
Here's a sample of the HTML that's being affected by the code
<div class="salafoot" id="salamander-8" style="opacity: 0;"></div>
As you can see I reuse almost identical code a lot of times, and if I want to change the number of steps (say to 37) I need to add another line. The if statements all get left open until the end, so that it only runs the check on the next line if it's useful (only checks +40 if the +20 has already been approved). All of the #salamander-(n)
IDs are tied to the .salafoot
class. The expected output is footprints that appear one at a time as you scroll down the page.