利用scrollTop做跳到页面指定位置

goTo = function(target){

                var scrollT = document.body.scrollTop|| document.documentElement.scrollTop

                if (scrollT >target) {

                    var timer = setInterval(function(){

                        var scrollT = document.body.scrollTop|| document.documentElement.scrollTop

                        var step = Math.floor(-scrollT/6);

                        document.documentElement.scrollTop = document.body.scrollTop = step + scrollT;

                        if(scrollT <= target){

                            document.body.scrollTop = document.documentElement.scrollTop = target;

                            clearTimeout(timer);

                        }

                    },20)

                }else if(scrollT == 0){

                    var timer = setInterval(function(){

                        var scrollT = document.body.scrollTop|| document.documentElement.scrollTop

                        var step = Math.floor(300/3*0.7);

                        document.documentElement.scrollTop = document.body.scrollTop = step + scrollT;

                        console.log(scrollT)

                        if(scrollT >= target){

                            document.body.scrollTop = document.documentElement.scrollTop = target;

                            clearTimeout(timer);

                        }

                    },20)

                }else if(scrollT < target){

                    var timer = setInterval(function(){

                        var scrollT = document.body.scrollTop|| document.documentElement.scrollTop

                        var step = Math.floor(scrollT/6);

                        document.documentElement.scrollTop = document.body.scrollTop = step + scrollT;

                        if(scrollT >= target){

                            document.body.scrollTop = document.documentElement.scrollTop = target;

                            clearTimeout(timer);

                        }

                    },20)

                }else if(target == scrollT){

                    return false;

                }

            }

你可能感兴趣的:(利用scrollTop做跳到页面指定位置)