jQuery
$('.totop').on('click', function () {
$('body,html').animate({
scrollTop: 0,
}, 80);
})
原生
var topBtn = document.querySelector('.totop');
function totop(speed, delay) {
var timer = null;
timer = setInterval(function () {
var currentScroll = document.documentElement.scrollTop || document.body.scrollTop;
document.documentElement.scrollTop = document.body.scrollTop = currentScroll - speed;
if (currentScroll == 0) {
clearInterval(timer);
}
}, delay);
}
topBtn.addEventListener('click', function () {
totop(50, 10);
})
网友评论