回到顶部这个功能完成的时候,回到顶部的动画效果好用了,但是在往下滑动滚动条的时候,发现滚动条不好用了,
只需要把点击事件写在外面就可以解决这个问题;
下面代码复制就可以查看效果;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
html,
body {
height: 3000px;
}
.business_foot {
width: 100px;
height: 100px;
background: pink;
border-radius: 50%;
position: fixed;
bottom: 20px;
right: 50px;
}
</style>
<body>
<div class="business_foot">
</div>
</body>
</html>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
function TopBack() {
this.top = $(".business_foot");
this.init();
}
TopBack.prototype = {
init() {
this.back();
},
back() {
$(document).on('scroll', $.proxy(this.backCb, this))
},
backCb() {
var scroll = $(document).scrollTop();
if (scroll > 300) {
this.top.css("display", 'block');
} else {
this.top.css("display", 'none')
}
},
clickBack() {
this.top.on('click', $.proxy(this.clickBackCb, this))
},
clickBackCb() {
$('body,html').animate({ scrollTop: 0 }, 1000);
}
}
new TopBack();
</script>