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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

原生JS实现移动端模块的左右滑动切换效果,基于vue、stylus

發(fā)布時間:2023/12/31 vue 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 原生JS实现移动端模块的左右滑动切换效果,基于vue、stylus 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原生JS實現(xiàn)移動端模塊的左右滑動動畫效果,基于vue、stylus

大概實現(xiàn)方案:

手指touch屏幕的整個過程,會派發(fā)touchstart、touchmove、touchend三個事件,對這三個事件設置相應函數(shù),通過移動過程中位置的變化計算出偏移值,進行對應的設置。

注:以下講解請忽略數(shù)據(jù)(可用兩個設置高度寬度的空dom代替),這篇博客只針對切換效果的實現(xiàn)

效果展示

①歌詞未左滑:


②歌詞左滑后:


所需數(shù)據(jù)定義:


解釋:currentShow用于標記當前顯示的模塊為CD模塊還是歌詞模塊、touchInfo對象用于保存touch事件的相關屬性

vue dom結構:


stylus:


原生JavaScript:

解釋:三個皆為相應dom中touch事件的方法,詳情請注釋即可。

middleTouchStart(e) {// touch開始時,將touchInfo對象設置為已初始化狀態(tài)this.touchInfo.initiated = true// 用來判斷是否是一次移動this.touchInfo.moved = falseconst touch = e.touches[0]// 記錄touch位置的橫坐標與縱坐標this.touchInfo.startX = touch.pageXthis.touchInfo.startY = touch.pageY},middleTouchMove(e) {if (!this.touchInfo.initiated) {return}const touch = e.touches[0]// 橫坐標與縱坐標的偏移const deltaX = touch.pageX - this.touchInfo.startXconst deltaY = touch.pageY - this.touchInfo.startYif (Math.abs(deltaY) > Math.abs(deltaX)) {return}if (!this.touchInfo.moved) {this.touchInfo.moved = true}// 判斷當前顯示的是cd還是歌詞,如果是cd,則當前左偏移值為0,否則偏移值為-window.innerWidthconst left = this.currentShow === 'cd' ? 0 : -window.innerWidth// 求偏移值const offsetWidth = Math.min(0, Math.max(-window.innerWidth, left + deltaX))// 求偏移值占可視區(qū)域的百分比,用于判斷是否應該切換顯示狀態(tài)this.touchInfo.percent = Math.abs(offsetWidth / window.innerWidth)// 移動時歌詞模塊的偏移效果this.$refs.lyricList.$el.style.transform = `translate3d(${offsetWidth}px,0,0)`this.$refs.lyricList.$el.style.transitionDuration = 0// 移動時CD模塊的淡出效果this.$refs.cd.style.opacity = 1 - this.touchInfo.percentthis.$refs.cd.style.transitionDuration = 0},middleTouchEnd() {if (!this.touchInfo.moved) {return}let offsetWidthlet opacityif (this.currentShow === 'cd') {// 移動百分比大于屏幕一半,則切換顯示狀態(tài)if (this.touchInfo.percent > 0.5) {offsetWidth = -window.innerWidthopacity = 0this.currentShow = 'lyric'} else {offsetWidth = 0opacity = 1}} else {if (this.touchInfo.percent < 0.5) {offsetWidth = 0this.currentShow = 'cd'opacity = 1} else {offsetWidth = -window.innerWidthopacity = 0}}// 最終狀態(tài)的設置// 動畫時間const time = 300// touch完畢后歌詞模塊應該放置的位置this.$refs.lyricList.$el.style.transform = `translate3d(${offsetWidth}px,0,0)`this.$refs.lyricList.$el.style.transitionDuration = `${time}ms`// touch完畢后CD模塊的透明度this.$refs.cd.style.opacity = opacitythis.$refs.cd.style.transitionDuration = `${time}ms`// 一次touch完成后,重置touchInfo對象尚未初始化狀態(tài)this.touchInfo.initiated = false}
至此,原生JS實現(xiàn)移動端模塊的左右滑動動畫效果實現(xiàn)完成。

總結

以上是生活随笔為你收集整理的原生JS实现移动端模块的左右滑动切换效果,基于vue、stylus的全部內容,希望文章能夠幫你解決所遇到的問題。

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