最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue2.0實(shí)現(xiàn)音樂/視頻播放進(jìn)度條組件

 更新時(shí)間:2021年09月03日 15:46:49   作者:ECMAScripter  
這篇文章主要為大家詳細(xì)介紹了vue2.0實(shí)現(xiàn)音樂和視頻播放進(jìn)度條組件的思路及具體實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

基于vue2.0實(shí)現(xiàn)音樂/視頻播放進(jìn)度條組件的方法及代碼解釋,具體內(nèi)容如下

需求分析:

①:進(jìn)度條隨著歌曲的播放延長,歌曲播放完時(shí)長度等于黑色總進(jìn)度條長度;時(shí)間實(shí)時(shí)更新。

②:當(dāng)滑動按鈕時(shí),實(shí)時(shí)更新播放時(shí)間,橙色進(jìn)度條長度也會隨著按鈕的滑動而改變,當(dāng)滑動結(jié)束時(shí),橙色區(qū)域停留在滑動結(jié)束的位置,歌曲從當(dāng)前進(jìn)度開始播放。

③:點(diǎn)擊進(jìn)度條,橙色進(jìn)度條長度變?yōu)辄c(diǎn)擊處至起點(diǎn)的長度,并從當(dāng)前點(diǎn)開始播放歌曲。

大概思路:

①:左邊的時(shí)間可以通過audio播放時(shí)派發(fā)的timeupdate事件獲取,右邊的時(shí)間為接口獲取的當(dāng)前歌曲的總時(shí)間。

②:進(jìn)度條子組件的長度通過父組件傳入一個(gè)percent值計(jì)算,percent值為播放進(jìn)度與總進(jìn)度的比值。

③:進(jìn)度條的滑動及點(diǎn)擊結(jié)束后,需要向父組件傳遞一個(gè)percent值,使用this.$emit()像父組件派發(fā)事件,父組件中設(shè)置事件響應(yīng)函數(shù),接收percent參數(shù)值,用于改變audio中當(dāng)前播放的音樂進(jìn)度。

詳細(xì)實(shí)現(xiàn),關(guān)鍵代碼已經(jīng)注釋:

先上組件源碼:

<template> 
 <div class="progress-bar" ref="progressBar" @click="progressClick"> 
  <div class="bar-inner"> 
   <div class="progress" ref="progress"></div> 
   <div class="progress-btn-wrapper"ref="progressBtn" 
      @touchstart.prevent = "progressTouchStart" 
      @touchmove.prevent = "progressTouchMove" 
      @touchend = "progressTouchEnd" 
   > 
    <div class="progress-btn"></div> 
   </div> 
  </div> 
 </div> 
</template> 
 
<script type="text/ecmascript-6"> 
 // 進(jìn)度條按鈕寬度,由于style中沒有設(shè)置width,因此只能用clientWidth獲取 
 export default { 
  data() { 
   return { 
    btnWidth: { 
     type: Number, 
     default: 0 
    }, 
    touchInfo: { 
     initiated: false 
    } 
   } 
  }, 
  props: { 
   percent: { 
    type: Number, 
    default: 0 
   } 
  }, 
  mounted() { 
   this.btnWidth = document.getElementsByClassName('progress-btn')[0].clientWidth 
  }, 
  methods: { 
   // 點(diǎn)擊按鈕 
   progressTouchStart(e) { 
    // 記錄touch事件已經(jīng)初始化 
    this.touchInfo.initiated = true 
    // 點(diǎn)擊位置 
    this.touchInfo.startX = e.touches[0].pageX 
    // 點(diǎn)擊時(shí)進(jìn)度條長度 
    this.touchInfo.left = this.$refs.progress.clientWidth 
   }, 
   // 開始移動 
   progressTouchMove(e) { 
    if (!this.touchInfo.initiated) { 
     return 
    } 
    // 計(jì)算移動距離 
    const moveX = e.touches[0].pageX - this.touchInfo.startX 
    // 設(shè)置偏移值 
    const offsetWidth = Math.min(Math.max(0, this.touchInfo.left + moveX), 
     this.$refs.progressBar.clientWidth - this.btnWidth) 
    this._setOffset(offsetWidth) 
   }, 
   // 移動結(jié)束 
   progressTouchEnd(e) { 
    this.touchInfo.initiated = false 
    // 向父組件派發(fā)事件,傳遞當(dāng)前百分比值 
    this._triggerPercent() 
   }, 
   // 進(jìn)度條點(diǎn)擊事件 
   progressClick(e) { 
    console.log('clikc') 
    // 設(shè)置進(jìn)度條及按鈕偏移 
    this._setOffset(e.offsetX) 
    // 通知父組件播放進(jìn)度變化 
    this._triggerPercent() 
   }, 
   _triggerPercent() { 
    const barWidth = this.$refs.progressBar.clientWidth - this.btnWidth 
    const percent = Math.min(1, this.$refs.progress.clientWidth / barWidth) 
    this.$emit('percentChange', percent) 
   }, 
   // 設(shè)置偏移 
   _setOffset(offsetWidth) { 
    // 設(shè)置進(jìn)度長度隨著百分比變化 
    this.$refs.progress.style.width = `${offsetWidth}px` 
    // 設(shè)置按鈕隨著百分比偏移 
    this.$refs.progressBtn.style.transform = `translate3d(${offsetWidth}px, 0, 0)` 
   } 
  }, 
  watch: { 
   // 監(jiān)聽歌曲播放百分比變化 
   percent(newPercent, oldPercent) { 
    if (newPercent > 0 && !this.touchInfo.initiated) { 
     // 進(jìn)度條總長度 
     const barWidth = this.$refs.progressBar.clientWidth - this.btnWidth 
     const offsetWidth = barWidth * newPercent 
     // 設(shè)置進(jìn)度條及按鈕偏移 
     this._setOffset(offsetWidth) 
    } 
   } 
  } 
 } 
</script> 
 
<style lang="stylus" rel="stylesheet/stylus"> 
 @import "~common/stylus/variable.styl" 
 
 .progress-bar 
  height 0.5rem 
  .bar-inner 
   position relative 
   top 0.2rem 
   height 0.08rem 
   background rgba(0, 0, 0, 0.3) 
   .progress 
    position absolute 
    height 100% 
    background $color-theme 
   .progress-btn-wrapper 
    position absolute 
    left -0.25rem 
    top -0.25rem 
    width 0.5rem 
    height 0.5rem 
    .progress-btn 
     position relative 
     top 0.12rem 
     left 0.12rem 
     box-sizing border-box 
     width 0.32rem 
     height 0.32rem 
     border 0.06rem solid $color-text 
     border-radius 50% 
     background $color-theme 
</style> 

此為progerss-bar.vue組件源碼,組件所需要父組件傳入的值只有一個(gè)“percent”,為父組件中audio當(dāng)前播放時(shí)間與總時(shí)間的比值,用于計(jì)算此組件中橙色進(jìn)度條的長度。

組件的使用:

首先導(dǎo)入并注冊組件(在此不做解釋),隨后使用此組件,dom:

<div class="progress-wrapper"> 
 <span class="time time-l">{{formatTime(currentTime)}}</span> 
 <div class="progress-bar-wrapper"> 
  <progress-bar :percent="percent" @percentChange="setProgress"></progress-bar> 
 </div> 
 <span class="time time-r">{{formatTime(currentSong.duration)}}</span> 
</div> 

解釋:兩個(gè)span為左右兩個(gè)時(shí)間值,progress-bar為調(diào)用的組件,需要傳入percent值,用于子組件設(shè)置進(jìn)度條長度
percent值來自于audio的currenTime與歌曲總長度的比值:

// 計(jì)算百分比 
percent() { 
 return Math.min(1, this.currentTime / this.currentSong.duration) 
} 

@percentChange為子組件中派發(fā)過來的事件,詳細(xì)請看子組件中源碼及注釋“_triggerPercent()”部分,此事件調(diào)用的方法用于接收子組件傳過來的拖動按鈕、點(diǎn)擊進(jìn)度條改變歌曲播放進(jìn)度后的播放百分比,用于改變父組件中audio標(biāo)簽的currentTime,進(jìn)而將歌曲播放進(jìn)度設(shè)置為當(dāng)前時(shí)間。

以下為父組件中,接收到子組件派發(fā)過來的事件后調(diào)用的函數(shù)。

// 設(shè)置進(jìn)度 
  setProgress(percent) { 
   // 根據(jù)子組件傳過來的百分比設(shè)置播放進(jìn)度 
   this.$refs.audio.currentTime = this.currentSong.duration * percent 
   // 拖動后設(shè)置歌曲播放 
   if (!this.playing) { 
    this.togglePlaying() 
   } 
  }, 

樣式(本人使用stylus):

.progress-wrapper 
     display flex 
     .time 
      font-size 0.24rem 
      &.time-l 
       position absolute 
       bottom 1.62rem 
       left 1rem 
      &.time-r 
       position absolute 
       bottom 1.62rem 
       right 1rem 
     .progress-bar-wrapper 
      position absolute 
      bottom 1.5rem 
      left 1.7rem 
      width 4.2rem 

至此,進(jìn)度條組件的實(shí)現(xiàn)及使用方法均介紹完畢。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue獲取表單數(shù)據(jù)的方法

    Vue獲取表單數(shù)據(jù)的方法

    這篇文章主要為大家介紹了Vue獲取表單數(shù)據(jù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • vue 插件的方法代碼詳解

    vue 插件的方法代碼詳解

    在開發(fā)項(xiàng)目的時(shí)候,我們一般都用 vue-cli 來避免繁瑣的 webpack 配置和 template 配置。這篇文章主要介紹了如何寫一個(gè) vue 插件,需要的朋友可以參考下
    2019-06-06
  • Vue3處理錯(cuò)誤邊界(error boundaries)的示例代碼

    Vue3處理錯(cuò)誤邊界(error boundaries)的示例代碼

    在開發(fā) Vue 3 應(yīng)用時(shí),處理錯(cuò)誤邊界(Error Boundaries)是一個(gè)重要的考量,在 Vue 3 中實(shí)現(xiàn)錯(cuò)誤邊界的方式與 React 等其他框架有所不同,下面,我們將深入探討 Vue 3 中如何實(shí)現(xiàn)錯(cuò)誤邊界,并提供一些示例代碼幫助理解什么是錯(cuò)誤邊界,需要的朋友可以參考下
    2024-10-10
  • 如何快速上手Vuex

    如何快速上手Vuex

    本文主要介紹了Javascript中Vuex的相關(guān)知識。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • Vue 2.0學(xué)習(xí)筆記之Vue中的computed屬性

    Vue 2.0學(xué)習(xí)筆記之Vue中的computed屬性

    本篇文章主要介紹了Vue 2.0學(xué)習(xí)筆記之Vue中的computed屬性,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • vue中el-dialog打開與關(guān)閉的幾種方式

    vue中el-dialog打開與關(guān)閉的幾種方式

    本文主要介紹了vue中el-dialog打開與關(guān)閉的幾種方式,包括 update:visible, ref和兄弟 bus這三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • 基于Vue3實(shí)現(xiàn)旋轉(zhuǎn)木馬動畫效果

    基于Vue3實(shí)現(xiàn)旋轉(zhuǎn)木馬動畫效果

    這篇文章主要為大家介紹了如何利用Vue3實(shí)現(xiàn)旋轉(zhuǎn)木馬的動畫效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Vue有一定的幫助,需要的可以參考一下
    2022-05-05
  • Vue的根路徑為什么不能作為跳板跳轉(zhuǎn)到其他頁面(問題診斷及方案)

    Vue的根路徑為什么不能作為跳板跳轉(zhuǎn)到其他頁面(問題診斷及方案)

    文章主要介紹了Vue應(yīng)用中路由配置錯(cuò)誤的診斷和解決方案,根路徑配置錯(cuò)誤和未正確使用`<router-view>`標(biāo)簽是常見的問題,會導(dǎo)致路由參數(shù)無法正常傳遞,感興趣的朋友跟隨小編一起看看吧
    2025-03-03
  • Vue實(shí)現(xiàn)文件批量打包壓縮下載

    Vue實(shí)現(xiàn)文件批量打包壓縮下載

    這篇文章主要為大家詳細(xì)介紹了如何利用Vue實(shí)現(xiàn)文件批量打包壓縮下載功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-07-07
  • vue+阿里的G2圖表-antv+折線圖實(shí)例

    vue+阿里的G2圖表-antv+折線圖實(shí)例

    這篇文章主要介紹了vue+阿里的G2圖表-antv+折線圖實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04

最新評論

龙州县| 邢台市| 雷山县| 遂平县| 卓尼县| 大兴区| 浮山县| 石楼县| 大兴区| 平顺县| 德惠市| 元朗区| 神木县| 湛江市| 洪雅县| 乌苏市| 济南市| 安徽省| 阿坝| 南宫市| 克东县| 睢宁县| 松溪县| 甘德县| 自贡市| 东台市| 嘉义市| 竹山县| 龙胜| 邓州市| 苗栗市| 永福县| 屯留县| 宝鸡市| 广德县| 罗甸县| 沈阳市| 大方县| 佛学| 萨嘎县| 玛沁县|