<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style>
.aaa{
height: 10000px;
background: #000000;
}
</style>
<body>
<div class="aaa">

</div>
<div class="totop" id="goto">
<i class="iconfont icon-arrow-up-fill"></i>
</div>
<script src="../js/huidaodingbu.js"></script>
<script type="text/javascript">
var gou=new goTop({
container:"goto"
})
</script>
</body>
</html>
js代码:
(function(w,d){
var defautSettings={
position:"fixed",
width:"35px",
height:"35px",
right:"30px",
bottom:"30px",
cursor:"pointer",
backgroudColor:"#7a6e6e",
borderRadius:"3px 0 0 3px" ,
}
function goTop(options){
var self=this;
self=Object.assign(self,options,defautSettings);
self._setStyle();
self._setEvent();
}
goTop.prototype={
_setStyle:function(){
var self=this;
var gotoDom=d.getElementById(self.container);
for(var i in self){
if(i!="container" && i!="_setStyle" && i!="_setEvent"){
gotoDom.style[i]=self[i];
}
}
},
_setEvent:function(){
var self=this;
var gotoDom=d.getElementById(self.container);
gotoDom.onclick=function(){
clearInterval(times);
var times = setInterval(function(){
if(d.documentElement.scrollTop > 0){
d.documentElement.scrollTop = d.documentElement.scrollTop - 10;
}else{
clearInterval(times);
}
},1);
}
}
}
w.goTop = goTop ;
})(window,document)

解决方案 »

  1.   


    _setEvent: function() {
    var self = this;
    var gotoDom = d.getElementById(self.container);
    var times;
    gotoDom.onclick = function() {
    clearInterval(times);
    times = setInterval(function() {
    if (d.documentElement.scrollTop > 0) {
    d.documentElement.scrollTop = d.documentElement.scrollTop - 10;
    } else {
    clearInterval(times);
    }
    }, 1);
    }
    }
    }