方法1
<script type="text/javascript">
function fun1() {
var btn = document.querySelector("button");
btn.addEventListener("click", function() {
var timer = setInterval(function() {
var curScroll = window.pageYOffset;
window.scrollTo(0, curScroll-20)
if (curScroll<=0) {
clearInterval(timer);
}
},1000/60)
})
}
fun1();
方法2
function fun2() {
var btn = document.querySelector("button");
btn.addEventListener("click", function(){
toTop();
})
}
function toTop() {
var curScroll = window.pageYOffset;
window.scrollTo(0, curScroll - 20);
var timer = window.requestAnimationFrame(toTop)
if(curScroll <= 0) {
cancelAnimationFrame(timer);
}
}
fun2();
</script>
网友评论