admin管理员组

文章数量:1399313

Does anyone have any idea why the following script does not reset the place of the page to the top? (The autoscrolling works fine) I am look for the fix to be patable in IE...

 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 pliant mode'
                    myWidth = document.documentElement.clientWidth;
                    myHeight = document.documentElement.clientHeight;
                } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                    //IE 4 patible
                    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);

Does anyone have any idea why the following script does not reset the place of the page to the top? (The autoscrolling works fine) I am look for the fix to be patable in IE...

 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 pliant mode'
                    myWidth = document.documentElement.clientWidth;
                    myHeight = document.documentElement.clientHeight;
                } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                    //IE 4 patible
                    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);
Share Improve this question asked Jul 18, 2011 at 15:55 OdinulfOdinulf 5812 gold badges7 silver badges19 bronze badges 1
  • Check your css - by me the problem was that I had 'overflow: scroll' set to a child in the body, and not to the body/html itself, so I had to animate the child div and not the body/html (so instead of $('body, html').animate({ scrollTop: 0 }, "slow"); I had to do $('.child-element').animate({ scrollTop: 0 }, "slow"); ) – tsveti_iko Commented Sep 30, 2016 at 14:33
Add a ment  | 

1 Answer 1

Reset to default 4

In your code you have:

document.body.scrollTop(0, 0);

scrollTop is not a method call.

Did you mean window.scrollTo(0,0) or document.body.scrollTop = 0;?

本文标签: javascriptScrolltop not workingStack Overflow