zoukankan      html  css  js  c++  java
  • vuevideo监听touch事件

    vue-video是基于 Vue 的简洁 HTML5 视频播放器组件,但是并没有监听touch事件,也就是说在移动端按键无效。

      本文讲述如何改写其vue组件,使其兼容移动端。只需要在其原有的mouse事件上,再额外添加touch事件即可。

      html部分

    <div class="__cov-video-container" @mouseenter="mouseEnterVideo" @mouseleave="mouseLeaveVideo" @touchstart.native="toggleContrlShow">
    <div class="__cov-contrl-video-slider" @click="slideClick" @mousedown="videoMove" @touchstart="videoMove">
    <div class="__cov-contrl-vol-slider" @click="volSlideClick" @mousedown="volMove" @touchstart="videoMove">

     js部分

    mounted () { //以前vedio版本钩子函数ready被替换成了mounted,此次需更改
        this.$nextTick(function () {
          // 代码保证 this.$el 在 document 中
          this.$video = this.$el.getElementsByTagName('video')[0]
          this.init()
          if (this.options.autoplay) {
            this.play()
          }
          //添加监听touch事件
          document.body.addEventListener('mousemove', this.mouseMoveAction, false)
          document.body.addEventListener('touchmove', this.mouseMoveAction, false)
          document.body.addEventListener('mouseup', this.mouseUpAction, false)
          document.body.addEventListener('touchend', this.mouseUpAction, false)
    
        })
      },
      beforeDestroy () {
          document.body.removeEventListener('mousemove', this.mouseMoveAction)
          document.body.removeEventListener('touchmove', this.mouseMoveAction)
          document.body.removeEventListener('mouseup', this.mouseUpAction)
          document.body.removeEventListener('touchend', this.mouseUpAction)
      },

     这样就可以实现了对移动端的兼容。

     如果需要在父组件调用该组件,推荐添加pause方法

    pause(){  //添加暂停
              this.$video.pause()
            },
    

     通过ref在父组件,调用子组件的方法。如

    this.$refs.myV[0].pause();
  • 相关阅读:
    4、自定义菜单
    3、关注、取消关注 与 关键字回复
    2、自动回复消息
    1、接入公众平台
    java学习备忘录
    vue组件最佳实践
    js拉起或下载app
    angular1.5 Components
    Charlse 使用小记
    2016年终总结
  • 原文地址:https://www.cnblogs.com/cjh1111/p/6947775.html
Copyright © 2011-2022 走看看