【问题标题】:ScrollTop not working in IEScrollTop 在 IE 中不起作用
【发布时间】:2011-10-07 21:18:58
【问题描述】:

有人知道为什么 scrollTop 在 IE 中不起作用?

它在 Chrome 中运行良好,但我不知道 firefox。 (这个脚本的想法是有一个自动滚动页面,一旦它到达页面底部就会重置)

function getheight() {

                var myWidth = 0,
            myHeight = 0;
             if (typeof (window.innerWidth) == 'number') {
                    //Non-IE
                    myWidth = window.innerWidth;
                    myHeight = window.innerHeight;
                } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
                    //IE 6+ in 'standards compliant mode'
                    myWidth = document.documentElement.clientWidth;
                    myHeight = document.documentElement.clientHeight;
                } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                    //IE 4 compatible
                    myWidth = document.body.clientWidth;
                    myHeight = document.body.clientHeight;
                }
                var scrolledtonum = window.pageYOffset + myHeight + 2;
                var heightofbody = document.body.offsetHeight;
                if (scrolledtonum >= heightofbody) {
                    document.body.scrollTop(0, 0);
                }
            }

            window.onscroll = getheight; 

            function func() {
                window.document.body.scrollTop++;
            }

            window.document.onmouseover = function () {
                clearInterval(interval);
            };

            window.document.onmouseout = function () {
                interval = setInterval(func, 20);
            };

            var interval = setInterval(func, 20);

【问题讨论】:

    标签: javascript html css internet-explorer dhtml


    【解决方案1】:

    试试:

    document.documentElement.scrollTop = x // where x is some integer
    

    【讨论】:

      【解决方案2】:

      试试这个

      window.scroll(0,0) //x轴,y轴

      【讨论】:

      • 如何制作动画或让它变慢
      【解决方案3】:

      这样的事情在一个或另一个浏览器上不起作用的原因通常是由于以下原因:

      window.document.body.scrollTop++;
      

      您不能只这样做,因为某些浏览器将该值作为字符串,例如“5px”,有些是数字。

      【讨论】:

        【解决方案4】:

        EDGE的解决方案需要在scrollingElement上设置scrollTop文档属性:

        document.scrollingElement.scrollTop= x; // x is integer value
        

        但您必须确保 HTML 元素上的 CSS 已将溢出设置为默认值(可见):

        html {
            overflow: visible;
        }
        

        【讨论】:

          【解决方案5】:

          如果您需要它在 IE 和 Edge 中工作:

          document.body.scrollTop = x
          

          【讨论】:

          • 没有 IE Edge 之类的东西
          猜你喜欢
          • 2018-04-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-16
          • 2012-11-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多