返回顶部JS代码,不兼容谷歌浏览器!谁能帮我看看?
JScript code
<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');
HTML code
<a id="gotopbtn" style="position:fixed;width:12px;bottom:95px;right:195px;display:none;cursor:pointer;">返回顶部</a>
------解决方案--------------------
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
</head>
<body style="height:1000px;">
<a id="gotopbtn" style="position:fixed;width:12px;bottom:95px;right:195px;display:none;cursor:pointer;">返回顶部</a>
<script>
var backTop = function(btnId){
var btn = document.getElementById(btnId);
var d = document.documentElement || document.body;
window.onscroll = set;
btn.onclick = function () {
btn.style.display = "none";
window.onscroll = null;
this.timer = setInterval(function () {
//console.log(document.body.scrollTop)
//console.log(document.documentElement.scrollTop)
if( document.body.scrollTop ){
document.body.scrollTop -= Math.ceil(document.body.scrollTop * 0.1);
}else{
document.documentElement.scrollTop -= Math.ceil(document.documentElement.scrollTop * 0.1);
}
if (document.body.scrollTop == 1) clearInterval(btn.timer), window.onscroll = set, document.body.scrollTop = 0;
if (document.documentElement.scrollTop == 1) clearInterval(btn.timer), window.onscroll = set, document.documentElement.scrollTop = 0;
}, 10);
};
function set() { btn.style.display = (document.documentElement.scrollTop || document.body.scrollTop) ? 'block' : "none" }
};
backTop('gotopbtn');
</script>
</body>
</html>
试试