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 }) } }