javascript - I.E 滚动时的 css 位置问题

标签 javascript css internet-explorer

我编写了一个 scrollSpy 函数来检测用户在网页上上下滚动时的事件。

<script type="text/javascript">
function yPos() {
  var pos = 0;
  if( typeof( window.pageYOffset ) == 'number' ){
    //Netscape compliant
    pos = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    pos = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    pos = document.documentElement.scrollTop;
  }
  return pos;
}

window.onscroll = function(){
  var scrollPos  = yPos(), goTopElem = document.getElementById('scroll'), docBody = document.getElementsByTagName('body')[0];
  if(goTopElem && scrollPos  < 500 )                // user has scrolled up
     goTopElem.parentNode.removeChild(goTopElem);   // remove go to top link

  else if(scrollPos  > 500 && !goTopElem){ 
    var newDiv = document.createElement('DIV'), newLink = document.createElement('A'), txt = document.createTextNode('[back to top]'); 

     newLink.setAttribute('href','javascript:scroll(0,0);');
     newLink.appendChild(txt);   
     newDiv.setAttribute('id','scroll');  
     newDiv.appendChild(newLink);
     docBody.appendChild(newDiv);
   }
 } 
 </script> 
 <style type="text/css"> 
#scroll {
  position:fixed;   
  right: 0px; 
  bottom: 0px;
  display: block;
}  
  </style>

问题出在 Internet Explorer 上,当向下滚动时,链接应该出现在窗口的右下角 - 但这种情况并没有发生。 请帮忙。

最佳答案

如果您使用的是 IE6 或更旧的 IE7,则不支持 position: fixed。如果不是,请更新问题,并提供有关您所谈论的 IE 版本的更多细节。

关于javascript - I.E 滚动时的 css 位置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3422978/

相关文章:

javascript - 从内部对象 javascript 设置字段值

jquery动画循环问题

html - 有 IE 错误问题 - CSS

jQuery 更改类 - div 在 Firefox 中消失,在 Chrome、IE、Safari 中正常

html - 为什么我的 CSS 框没有改变颜色?

css - HTML5 Canvas 作为背景

internet-explorer - 带有 :after 和 :after::hover 伪类的 Internet Explorer 奇怪的字体大小行为

javascript - 如何使我的网页页脚处于 100% 高度?

javascript - gjs 中的异步代码在调用回调之前退出

javascript - Angularjs Bootstrap 分页设置页面不触发页面更改