2

Trying to move a position:fixed div on scroll by changing the top: css value in javascript. The div won't move though, not sure why.

html:

<div id="red">
  <div id="blue"></div>
</div>

css:

#red {
  position: relative;
  float: left;
  width: 100%;
  height: 1000px;
  background: rgba(255, 0, 0, 0.2);
  border: solid 2px #f0f;
}

#blue {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 50vh;
  background: rgba(0, 0, 255, 0.2);
  border: solid 2px #0ff;
}

js:

window.addEventListener('scroll', function() {
  var yPos = -(Math.floor(document.body.scrollTop / 10));
  //console.log("yPos = " + yPos); //output is correct
  document.getElementById('blue').style.top = yPos + 'px';
  //document.getElementById('blue').setAttribute('top',yPos); //also tried this
});

https://jsfiddle.net/akzx43yL/

Why isn't the top css value changing and how can I get it to do so? No jquery please.

4
  • 1
    First off you definitely want to debounce your scroll event or you're browser will likely break out a sweat, next why don't you just change from position fixed to absolute when you start scrolling? Commented Dec 8, 2017 at 2:18
  • 1
    = yPos + 'px'; Commented Dec 8, 2017 at 2:21
  • 1
    See jsfiddle.net/sjac0w67 . Seems like the .style.top = yPos in the js isn't actually having any effect regardless of the position style of the element. Commented Dec 8, 2017 at 2:22
  • @JaromandaX thanks, adding to question, but still no cigar. Commented Dec 8, 2017 at 2:24

3 Answers 3

2

Two things:

  • Instead of document.documentElement.scrollTop, you should use window.pageYOffset (scrollTop doesn't play nicely in Chrome).
  • You need to add a unit of measurement after you update top; values other than 0 should have px appened to them.

This can be seen in the following:

window.addEventListener('scroll', function() {
  var yPos = -(Math.floor(window.pageYOffset / 10));
  document.getElementById('blue').style.top = yPos + "px";

  // Optionally log the `top` value
  //console.log(document.getElementById('blue').style.top);
});
#red {
  position: relative;
  float: left;
  width: 100%;
  height: 1000px;
  background: rgba(255, 0, 0, 0.2);
  border: solid 2px #f0f;
}

#blue {
  position: absolute;
  top: -10px;
  left: 0;
  width: 100vw;
  height: 50vh;
  background: rgba(0, 0, 255, 0.2);
  border: solid 2px #0ff;
}
<div id="red">
  <div id="blue"></div>
</div>

Hope this helps! :)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but it still doesn't look like blue is moving 10 times slower than scroll speed as described here: window.pageYOffset / 10... or am I missing something? See: jsfiddle.net/vptjxdyo
Your top value is still zero in your example. Your line of code document.getElementById('blue').style.top += yPos + 'px' won't work; you can't do addition with strings. Simply remove the + before the =; the value is updated automatically anyway :)
Okay but why isn't this working then: jsfiddle.net/200xjj8j ? Notice how the blue element does not scroll 1000 times slower than the red here... instead it scrolls normally as any absolutely positioned element would without any js.
You can't divide by 1000. I'm not really sure why, but that still doesn't update the top value. If you take the exact same code in your fiddle, and divide by 10 instead of 1000, it will work. Though note that you probably want to increase the value rather than decrease it :)
Oooooooh lol I thought dividing by 1000 would make it more noticeable for some reason. THANK YOU!
1

If you check your console, you will be see your console.log("yPos = " + yPos) is always 0 you most update your code as follow:

window.addEventListener('scroll', function() {
    var yPos = -(Math.floor(document.documentElement.scrollTop / 10));
    console.log("yPos = " + yPos);
    document.getElementById('blue').style.top = yPos + "px";
});

Tip:
Ways to get srollTop (pure js):

var top  = window.pageYOffset || document.documentElement.scrollTop;

Comments

0

This is what worked for me:

document.getElementById('blue').style.top = yPos + "px";

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.