Moving Div with Scroll

后端 未结 1 1178
陌清茗
陌清茗 2020-12-15 09:01

I have to move a div when the scroll bar moves, but need to use pure javascript, and position:fixed will not work with the layout. The div\'s original poisition is relative

相关标签:
1条回答
  • 2020-12-15 09:41
    window.onscroll = function (e) {
      var vertical_position = 0;
      if (pageYOffset)//usual
        vertical_position = pageYOffset;
      else if (document.documentElement.clientHeight)//ie
        vertical_position = document.documentElement.scrollTop;
      else if (document.body)//ie quirks
        vertical_position = document.body.scrollTop;
    
      var your_div = document.getElementById('some_div');
      your_div.top = (vertical_position + 200) + 'px';//200 is arbitrary.. just to show you could now position it how you want
    }
    
    0 讨论(0)
提交回复
热议问题