ScrollTop not working in IE
Asked Answered
P

5

11

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++;
            }

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

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

            var interval = setInterval(func, 20);
Pester answered 18/7, 2011 at 17:32 Comment(0)
G
23

Try:

document.documentElement.scrollTop = x // where x is some integer
Gironde answered 18/7, 2011 at 17:36 Comment(0)
R
6

Try this

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

Rachael answered 18/10, 2011 at 8:28 Comment(1)
How to animate it or make it go slowKilocalorie
J
2

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.

Jorum answered 18/7, 2011 at 17:37 Comment(0)
D
1

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;
}
Diversiform answered 9/8, 2019 at 6:45 Comment(0)
H
-1

If you need it to work in IE and Edge:

document.body.scrollTop = x
Heteroecious answered 21/9, 2018 at 13:42 Comment(1)
There's no such thing as IE EdgeBomb

© 2022 - 2024 — McMap. All rights reserved.