scrollto 到指定位置

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属性可能没什么作用,但是这样写的话兼容性就有保障

滚动到指定容器的底部

window.scrollTo(0,document.querySelector("#answer-11715670").scrollHeight);

 vue的写法

watch: {
    messages() {
      this.$nextTick(() => {
        let list = this.$els.xx
        list.scrollTop = list.scrollHeight
      })
    }
  }

 

posted on 2017-11-30 15:31  杨龙飞  阅读(391)  评论(0编辑  收藏  举报

导航

Fork me on GitHub