Home >Web Front-end >HTML Tutorial >Return to the top JS code, not compatible with Google Chrome! Can anyone help me take a look? _html/css_WEB-ITnose

Return to the top JS code, not compatible with Google Chrome! Can anyone help me take a look? _html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:19:371075browse

This post was last edited by liyueyang165 on 2012-06-12 12:46:28

<script type="text/javascript">        backTop = function (btnId){        var btn = document.getElementById(btnId);        var d = document.documentElement;        window.onscroll = set;        btn.onclick = function () {            btn.style.display = "none";            window.onscroll = null;            this.timer = setInterval(function () {                d.scrollTop -= Math.ceil(d.scrollTop * 0.1);                if (d.scrollTop == 0) clearInterval(btn.timer, window.onscroll = set);            }, 10);        };        function set() { btn.style.display = d.scrollTop ? 'block' : "none" }    };    backTop('gotopbtn');


<a id="gotopbtn" style="position:fixed;width:12px;bottom:95px;right:195px;display:none;cursor:pointer;">返回顶部</a>

Reply to the discussion ( Solution)

Chrome and Safari always use document.body.scrollTop, while IE and Firefox use document.body.scrollTop for quirks mode and document.documentElement.scrollTop for standard mode.
Your best bet may be something like:

var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
var scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;

Chrome and Safari always use document.body.scrollTop, while IE and Firefox use document.body.scrollTop for quirks mode and document.documentElement.scrollTop for standard mode.
Your best bet may be……

How do I modify this? .

backTop = function (btnId) {                var btn = document.getElementById(btnId);                var d1 = document.documentElement;                var d2 = document.body;                var d = d1.scrollTop + d2.scrollTop;                window.onscroll = set;                btn.onclick = function () {                    btn.style.display = "none";                    window.onscroll = null;                    this.timer = setInterval(function () {                        d -= Math.ceil(d* 0.1);                        if (d == 0) clearInterval(btn.timer, window.onscroll = set);                    }, 10);                };                function set() { btn.style.display = d ? 'block' : "none" }            };            backTop('gotopbtn');

Try this

JScript code
backTop = function (btnId) {
var btn = document.getElementById(btnId);
            var d1 = document.documentElement;
var d2 = document.body;
……

Not working. . Directly hidden and not displayed

Quote from the 4th floor’s reply:

Quote from the 3rd floor’s reply:

JScript code
backTop = function (btnId) {
var btn = document.getElementById(btnId);
var d1 = document.documentElement;
var d2 = document.body;



Not working. . Directly hidden...

Do not return to the home page.

fef50554eca1a427827adaa329da8122
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
93650611ffd0f4796331c58bd91517b0
b2386ffb911b14667cb8f0f91ea547a7683b85afec769a2dae32e320b747e360

9c3bca370b5104690d9ef395f2c5f8d1
61855187984f4ce5436f3b9484a2120b
fef50554eca1a427827adaa329da8122
100db36a723c770d327fc0aef2ce13b1 > 93f0f5c25f18dab9d176bd4f6de5d30e
93650611ffd0f4796331c58bd91517b0
b2386ffb911b14667cb8f0f91ea547a76e916e0f7d1e588d4f442bf645aedb2f

9c3bca370b5104690d9ef395f2c5f8d1
064048af9d81273c8702425eff8bf781
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
93650611ffd0f4796331c58bd91517b0
b2386ffb911b14667cb8f0f91ea547a7< ;/title>

9c3bca370b5104690d9ef395f2c5f8d1
61855187984f4ce5436f3b9484a2120b