纯js实现回到锚点

 window.requestAnimationFrame = window.requestAnimationFrame || function (fn) {
            return setTimeout(fn, 1000 / 60);
        };
        var target = document.getElementById("plist").offsetTop;
        if (location.href.indexOf('#plist') > 0 && parseInt(target) > 0) {
            (function animation() {
                var init = 0;
                (function fn() {
                    init += 40;
                    if (init >= target) {
                        init = target;
                    } else {
                        requestAnimationFrame(fn);
                    }
                    document.body.scrollTop = document.documentElement.scrollTop = init;
                })();
            })();
        }

 

posted @ 2018-02-11 16:26  DR19  阅读(268)  评论(0编辑  收藏  举报