0

I was executing code about window scrollTop where as you scroll up by 200 you have a div with one behaviour and if you cross 500px you would have div behave in another way. The later is not getting executed but just first one is executing. I am starting to learn javascript so please forgive me if this is a small fix :) Here's the code:

       window.onscroll = function() {myFunction()};

    function myFunction() {
        if(document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
            document.getElementById("meBox").style.position = "fixed";
            document.getElementById("meBox").style.background = "green";
        } else if (document.body.scrollTop > 500 || document.documentElement.scrollTop > 500) {   
            document.getElementById("meBox").style.position = "fixed";
            document.getElementById("meBox").style.background = "pink";

         } else {
            document.getElementById("meBox").style.position = "";
            document.getElementById("meBox").style.background = "";
            }

     }//end function
      
    *{margin: 0; padding: 0;}

  body {height: 3000px;}

    .box {float: left; width: 100%; height: 70px; background: yellow; padding: 15px; box-sizing: border-box; margin: 0; position: relative;}
    .box h2 {margin: 0; padding: 0;}
<div class="box" id="meBox"><h2>I am Heading</h2></div>

Please help!

Thanks in advance

3
  • 1
    It's because the first one get evaluated first and is always true, if it's bigger than 500 then it's bigger than 200 too. Put the if with 500 first. Commented Mar 7, 2016 at 12:52
  • Because 500 is greater than 200. If the first one is true, it will not keep checking the others. Commented Mar 7, 2016 at 12:53
  • put the > 500 in the if. then the > 200 in the else if Commented Mar 7, 2016 at 12:54

2 Answers 2

1

It's because the first one get evaluated first and is always true, if it's bigger than 500 then it's bigger than 200 too. Put the if with 500 first.

Also no need to wrap the myFunction in another function to assign it to onscroll.

window.onscroll = myFunction;

function myFunction() {
    if (document.body.scrollTop > 500 || document.documentElement.scrollTop > 500) {   
        document.getElementById("meBox").style.position = "fixed";
        document.getElementById("meBox").style.background = "pink";

     } else if(document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
        document.getElementById("meBox").style.position = "fixed";
        document.getElementById("meBox").style.background = "green";
    } else {
        document.getElementById("meBox").style.position = "";
        document.getElementById("meBox").style.background = "";
    }

}//end function
Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to answer the same :D
0

Improve the conditions i.e. specify the upper-limit also

window.onscroll = function() {
  myFunction()
};

function myFunction() {
    var top = document.body.scrollTop;
    var elemTop = document.documentElement.scrollTop;
    if (top > 200 && top <= 500 || elemTop > 200 && elemTop <= 500) {
      document.getElementById("meBox").style.position = "fixed";
      document.getElementById("meBox").style.background = "green";
    } else if (top > 500 || elemTop > 500) {
      document.getElementById("meBox").style.position = "fixed";
      document.getElementById("meBox").style.background = "pink";
    } else {
      document.getElementById("meBox").style.position = "";
      document.getElementById("meBox").style.background = "";
    }

  } //end function
* {
  margin: 0;
  padding: 0;
}
body {
  height: 3000px;
}
.box {
  float: left;
  width: 100%;
  height: 70px;
  background: yellow;
  padding: 15px;
  box-sizing: border-box;
  margin: 0;
  position: relative;
}
.box h2 {
  margin: 0;
  padding: 0;
}
<div class="box" id="meBox">
  <h2>I am Heading</h2>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.