ScrollTop无法在IE中运行

问题描述 投票:10回答:4

任何人有任何想法为什么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
4个回答
19
投票

尝试:

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

5
投票

试试这个

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


2
投票

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

window.document.body.scrollTop++;

你不能这样做,因为有些浏览器将这个值作为字符串,例如“5px”,有些人将它作为一个数字。


0
投票

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

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

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

html {
    overflow: visible;
}

-1
投票

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

document.body.scrollTop = x
© www.soinside.com 2019 - 2024. All rights reserved.