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‘);
将上面代码直接复制到最后的