开发者

ScrollTop not working in IE

Anyone have any ideas why scrollTop isn't working in IE?

It works in Chrome fine, and I don't know about firefox. (The idea of this script is to have an autoscrolling page that resets once it hits the bottom of the page)

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++;
  开发者_如何学JAVA          }

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

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

            var interval = setInterval(func, 20);


Try:

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


Try this

window.scroll(0,0) //x-axis, y-axis


The reason things like this don't work on one browser or another is usually due to something like:

window.document.body.scrollTop++;

You can't just do that because some browsers have that value as a string, e.g. "5px" and some have it as a number.


Solution for EDGE needs to set scrollTop on scrollingElement document property:

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

But you have to ensure, that CSS on HTML element has set overflow to default value (visible):

html {
    overflow: visible;
}


If you need it to work in IE and Edge:

document.body.scrollTop = x
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜