【问题标题】:Moving a div with page scroll使用页面滚动移动 div
【发布时间】:2012-11-10 03:32:14
【问题描述】:

在此Stack Overflow post 之后,我试图在向下滚动网页时使 div 浮动,但它对我不起作用。

下面是代码sn-p

<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>

【问题讨论】:

    标签: javascript


    【解决方案1】:

    您根本不需要 JavaScript,只需在 CSS 中使用 position: fixed;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      相关资源
      最近更新 更多