返回顶部

2019-11-26  本文已影响0人  Devin_tao

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);
        })
上一篇下一篇

猜你喜欢

热点阅读