000
000

Reputation: 3950

Moving a div with page scroll

Following this Stack Overflow post, i am trying to make a div float as i scroll down the webpage but it's not working for me.

Below is the code snippet

<script type="text/javascript">
window.onscroll = function (e) {
  var vertical_position = 0;
  if (pageYOffset)//usual
    vertical_position = pageYOffset;
  else if (document.documentElement.clientHeight)
    vertical_position = document.documentElement.scrollTop;
  else if (document.body)
    vertical_position = document.body.scrollTop;

  var your_div = document.getElementById('menuDiv');
  your_div.top = (vertical_position + 200) + 'px';
}
</script>

Upvotes: 0

Views: 1974

Answers (1)

Dai
Dai

Reputation: 155648

You don't need JavaScript at all, just use position: fixed; in CSS.

#menuDiv {
    position: fixed;
    top: 200px;
}

Upvotes: 3

Related Questions