美文网首页
利用scrollTop做跳到页面指定位置

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

作者: easy_mark | 来源:发表于2018-06-25 15:11 被阅读0次

    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做跳到页面指定位置

          本文链接:https://www.haomeiwen.com/subject/mjsyyftx.html