日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

总结VUE控制滚动滑动方法

發(fā)布時(shí)間:2024/1/8 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 总结VUE控制滚动滑动方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前景

由于VUE使用虛擬dom,導(dǎo)致滾動(dòng)條滑動(dòng)到具體元素非常生硬,在github上看到一位作者寫(xiě)了不錯(cuò)的方法因此記載下來(lái)分享

作者地址

傳送們

代碼

/** y: the y coordinate to scroll, 0 = top* duration: scroll duration in milliseconds; default is 0 (no transition)* element: the html element that should be scrolled ; default is the main scrolling element*/ function scrollToY (y, duration = 0, element = document.scrollingElement) {// cancel if already on target positionif (element.scrollTop === y) return;const cosParameter = (element.scrollTop - y) / 2;let scrollCount = 0, oldTimestamp = null;function step (newTimestamp) {if (oldTimestamp !== null) {// if duration is 0 scrollCount will be InfinityscrollCount += Math.PI * (newTimestamp - oldTimestamp) / duration;if (scrollCount >= Math.PI) return element.scrollTop = y;element.scrollTop = cosParameter + y + cosParameter * Math.cos(scrollCount);}oldTimestamp = newTimestamp;window.requestAnimationFrame(step);}window.requestAnimationFrame(step); }/** duration: scroll duration in milliseconds; default is 0 (no transition)* this function is using the scrollToY function*/ function scrollToTop(duration = 0) {scrollToY(0, duration, document.scrollingElement); }/** id: the id of the element as a string that should be scrolled to* duration: scroll duration in milliseconds; default is 0 (no transition)* this function is using the scrollToY function on the main scrolling element*/ function scrollToId(id, duration = 0, customOffset = 0) {const offset = Math.round(document.getElementById(id).getBoundingClientRect().top);scrollToY(document.scrollingElement.scrollTop + offset + customOffset, duration); }/** element: the node object that should be scrolled to* duration: scroll duration in milliseconds; default is 0 (no transition)* this function is using the scrollToY function on the main scrolling element*/ function scrollToElement(element, duration = 0, customOffset = 0) {const offset = Math.round(element.getBoundingClientRect().top);scrollToY(document.scrollingElement.scrollTop + offset + customOffset, duration); }

總結(jié)

以上是生活随笔為你收集整理的总结VUE控制滚动滑动方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。