第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【金秋打卡】第24天 拖拽進(jìn)度和音量條控制

標(biāo)簽:
Typescript

课程章节:第5章 弹出层中的Video播放器组件开发 5.8

课程讲师:西门老舅

课程内容:

今天学习的内容是实现进度条的拖拽播放,以及音量调节。

拖拽播放

核心是使用原生 JS 模拟拖拽事件。

进度条有一个小球,当鼠标点击它时,记录下此刻的位置,然后监听鼠标的移动,当鼠标松开时,记录下此刻小球的位置。通过这两个位置,得出滑块移动的距离,再根据占完整进度条的比例,计算出视频的实时时间,通过前面学过的 vide0.currentTime 进行设置。

 handle() {
    // 进度条容器
    let videoProcess = this.tempContainer.querySelector('.video-progress') as HTMLElement

    // 进度条位置
    let currentProcess = this.tempContainer.querySelector('.video-progress-now') as HTMLElement
    let sucProcess = this.tempContainer.querySelector('.video-progress-suc') as HTMLElement
    let barProcess = this.tempContainer.querySelector('.video-progress-bar') as HTMLElement

    // 拖拽播放
    barProcess.addEventListener('mousedown', function(event: MouseEvent) {
      let downX = event.pageX
      let downL = barProcess.offsetLeft
      
      // 监听鼠标移动事件,模拟拖拽
      // 方便移除事件监听,使用 DOM 0级事件绑定。但是最好使用 DOM2 级事件绑定
      document.onmousemove = function (event: MouseEvent) {
        let scale = (event.pageX - downX + downL + 8) / videoProcess.offsetWidth
        if(scale < 0) {
          scale = 0
        } else if(scale > 1) {
          scale = 1
        }

        // 设置实时进度
        currentProcess.style.width = scale * 100 + '%'
        sucProcess.style.width = scale * 100 + '%'
        barProcess.style.left = scale * 100 + "%"
        videoElm.currentTime = scale * videoElm.duration
      }

      // 松开鼠标时移除事件绑定
      document.onmouseup = function () {
        document.onmousemove = null
        document.onmouseup = null
      }

      event.preventDefault()
    })
  }
}

音量调节

video 元素提供了音量有关的 API, video.volume ,取值范围是 0~1,0表示静音,1 表示声音最大。

在拖拽音量滑块时,通过监听鼠标的移动事件,获得滑块实时位置,计算出此时所占音量调节器整体的比例,计算出值设置给 video.volume 即可。

handle() {

    // 音量调节容器 
    let volumeProcess = this.tempContainer.querySelector('.video-volume-progress') as HTMLElement
    let curVolumeProcess = this.tempContainer.querySelector('.video-volume-progress-now') as HTMLElement
    let barVolumeProcess = this.tempContainer.querySelector('.video-volume-progress-bar') as HTMLElement
    
    // 音量调节    
    barVolumeProcess.addEventListener('mousedown', function(event: MouseEvent) {
      let downX = event.pageX
      let downL = barVolumeProcess.offsetLeft
      
      document.onmousemove = function (event: MouseEvent) {
        let scale = (event.pageX - downX + downL + 8) / volumeProcess.offsetWidth
        if(scale < 0) {
          scale = 0
        } else if(scale > 1) {
          scale = 1
        }

        curVolumeProcess.style.width = scale * 100 + '%'
        barProcess.style.left = scale * 100 + "%"
        videoElm.volume = scale
      }

      document.onmouseup = function () {
        document.onmousemove = null
        document.onmouseup = null
      }

      event.preventDefault()
    })
}

课程收获

这节课实现了拖拽进度条播放功能,和音量调节功能,核心都是一致的,就是模拟拖拽操作,通过监听鼠标移动,计算元素位置的变化的比例,以此来设置实时的播放进度和音量。
图片描述

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消