vue h5手机端 图片随意拖拽触摸事件

  <div id="parent" style="width: 100%;height: 100%;position: relative">
        <div
            @touchmove.prevent="touchmove"
            @touchend.prevent="touchend"
            @touchstart.prevent="touchstart"
            @click="sample" id="child"
             class="draggable-item" >
          
        </div>
      </div>

拖拽部分的样式为,因为一进来的时候需要定位在一个地方,然后在随意拖拽不超出当前当前页面

.draggable-item{
  width: 168px;
  height: 137px;
  background-image: url("../../assets/img/menu/hang.png");
  background-size: 100% 100%;
  position: absolute;
  right: 27px;
  top: 356px;
  z-index: 99999;
}

然后就是触摸的时候的代码了

  touchend(){
      let self = this;
      clearTimeout(self.Loop);
      if (self.Loop !== 0) {
        alert('点击事件')
        this.sample()
      }
      return false;
    },
    touchstart(event){
      let child = document.getElementById('child')
      this.startX = event.targetTouches[0].pageX - child.offsetLeft;
      this.startY = event.targetTouches[0].pageY - child.offsetTop;
      let self = this;
      //执行长按的内容
      self.Loop = setTimeout(function () {
        self.Loop = 0;
      }, 200);
      return false;
    },
    touchmove(event){
      let child = document.getElementById('child')
      event.preventDefault()
      event.stopPropagation()
      this.leftX = event.targetTouches[0].pageX - this.startX;
      this.topY = event.targetTouches[0].pageY - this.startY;
      this.maxX = this.maxW - parseInt(window.getComputedStyle(child).width)-5
      this.maxY = this.maxH - parseInt(window.getComputedStyle(child).height)-5
      if (this.leftX < 0) {
        this.leftX = 0
      } else if (this.leftX > this.maxX) {
        this.leftX = this.maxX
      }
      if (this.topY < 0) {
        this.topY = 0
      } else if (this.topY > this.maxY) {
        this.topY = this.maxY
      }
      child.style.left = this.leftX + "px";
      child.style.top = this.topY + "px";
    },

如果遇到触摸事件和点击事件发生冲突,请看另一篇文章vue h5页面 解决touchstart,touchmove,touchend,click事件触发产生的问题_茂菇凉的博客-CSDN博客

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue H5触摸翻页是指在使用Vue框架开发H5网页时,通过触摸屏幕来实现页面切换的功能。下面是具体实现过程: 首先,需要在Vue项目的入口文件中引入Vue的touch插件,可以使用npm命令或直接在项目中引入: ``` npm install vue-touch ``` 接下来,在入口文件main.js中引入Vue的touch插件: ```javascript import VueTouch from 'vue-touch' Vue.use(VueTouch, {name: 'v-touch'}) ``` 在Vue文件中,可以通过绑定v-touch指令来监听触摸事件,具体实现如下: ```html <template> <div v-touch:swipeleft="changePage" v-touch:swiperight="changePage"> // 页面内容 </div> </template> <script> export default { methods: { changePage(event) { // 在这里进行页面切换的逻辑处理 // 可以根据事件类型(event.type)来判断页面切换方式 // 可以使用Vue Router进行页面切换 } } } </script> ``` 上述代码中,通过在div元素上绑定v-touch指令,并监听swipeleft和swiperight事件,当用户在移动端向左或向右滑动时,会触发changePage方法,在该方法中可以编页面切换的逻辑处理代码。 在changePage方法中,可以根据事件类型(event.type)来判断用户是向左滑动还是向右滑动,然后可以根据具体需求,使用Vue Router进行页面的切换,例如: ```javascript import router from '@/router' // 向左滑动,切换到下一页 if (event.type === 'swipeleft') { router.push('/nextPage') // 跳转到下一页 } // 向右滑动,切换到上一页 if (event.type === 'swiperight') { router.push('/prevPage') // 跳转到上一页 } ``` 通过以上实现代码,就可以在Vue H5项目中实现基于触摸事件的页面翻页功能了。具体页面切换效果和逻辑可以根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值