Vue实现鼠标拖拽

Vue实现鼠标拖拽

要实现拖拽,必须要使用三大秘法:(pc端)

1、鼠标按下事件:onmousedown

2、鼠标移动事件:onmousemove

3、鼠标抬起事件:onmouseup

移动端拖拽:

1、当在屏幕上按下手指时触发:touchstart

2、当在屏幕上移动手指时触发:touchmove

3、当在屏幕上抬起手指时触发:touchend

4、touchcancel 当一些更高级别的事件发生的时候(如电话接入或者弹出信息)会取消当前的touch操作,即触发
touchcancel。一般会在touchcancel时暂停游戏、存档等操作。

实现鼠标拖动的原理:

1、当鼠标点击下去的时候我们需要获取鼠标所在位置的横纵坐标,然后获取盒子的离页面的横纵方向的距离
2、计算出鼠标相对盒子的距离
3、当鼠标移动的时候,获取鼠标移动的距离,在永鼠标此刻的位置减去鼠标相对盒子的距离,获得的是盒子此刻的坐标位置
4、将这个位置赋值给盒子
5、鼠标抬起,清除鼠标移动事件;

接下来我们上代码
<template>
  <div class="home">
    <div id="box" v-drag></div>
  </div>
</template>

<script>
// @ is an alias to /src

export default {
  name: "Home",
  data() {return {}},
  directives: {
    drag: {
      // 指令的定义
      inserted: function(el) {
        // el.drag();
        console.log(el);
        //获取元素
        // var dv = document.getElementById("dv");
        let x = 0;
        let y = 0;
        let l = 0;
        let t = 0;
        let isDown = false;
        //鼠标按下事件
        el.onmousedown = function(e) {
          //获取x坐标和y坐标
          x = e.clientX;
          y = e.clientY;

          //获取左部和顶部的偏移量
          l = el.offsetLeft;
          t = el.offsetTop;
          //开关打开
          isDown = true;
          //设置样式
          el.style.cursor = "move";
        };
        //鼠标移动
        window.onmousemove = function(e) {
          if (isDown == false) {
            return;
          }
          //获取x和y
          let nx = e.clientX;
          let ny = e.clientY;
          //计算移动后的左偏移量和顶部的偏移量
          let nl = nx - (x - l);
          let nt = ny - (y - t);

          el.style.left = nl + "px";
          el.style.top = nt + "px";
        };
        //鼠标抬起事件
        el.onmouseup = function() {
          //开关关闭
          isDown = false;
          el.style.cursor = "default";
        };
      }
    }
  }
};
</script>
<style lang="scss" scoped>
#box {
  width: 60px;
  height: 60px;
  background-color: darkorange;
  border-radius: 50%;
  position: absolute;
  //脱离文档流
}
</style>
App实现鼠标拖拽

移动端实现拖拽,必须要使用三大秘法:

4、touchcancel 当一些更高级别的事件发生的时候(如电话接入或者弹出信息)会取消当前的touch操作,即触发
touchcancel。一般会在touchcancel时暂停游戏、存档等操作。
  • 10
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现鼠标轮播,可以结合 Vue 和第三方插件来完成。以下是一个基于 Vuevue-awesome-swiper 插件实现鼠标轮播的示例: 1. 安装 vue-awesome-swiper 插件 ``` npm install vue-awesome-swiper --save ``` 2. 在 Vue 组件中引入并初始化 swiper 组件 ``` <template> <div class="swiper-container" ref="swiper"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="(item, index) in list" :key="index"> <img :src="item.imgUrl" alt=""> </div> </div> </div> </template> <script> import Vue from 'vue' import VueAwesomeSwiper from 'vue-awesome-swiper' Vue.use(VueAwesomeSwiper) export default { data () { return { list: [ { imgUrl: 'http://xxx.com/1.jpg' }, { imgUrl: 'http://xxx.com/2.jpg' }, { imgUrl: 'http://xxx.com/3.jpg' }, { imgUrl: 'http://xxx.com/4.jpg' } ] } }, mounted () { this.$nextTick(() => { this.swiper = new this.$swiper(this.$refs.swiper, { slidesPerView: 'auto', freeMode: true, freeModeMomentumRatio: 0.5, freeModeMomentumBounce: false, freeModeMinimumVelocity: 0.1, grabCursor: true, mousewheel: true, resistanceRatio: 0.1 }) }) }, beforeDestroy () { if (this.swiper) { this.swiper.destroy() } } } </script> <style lang="scss"> .swiper-container { width: 100%; height: 100%; .swiper-wrapper { display: flex; .swiper-slide { width: 33.33%; img { width: 100%; } } } } </style> ``` 3. 在 mounted 钩子中初始化 swiper 实例,并设置相关参数。 ``` mounted () { this.$nextTick(() => { this.swiper = new this.$swiper(this.$refs.swiper, { slidesPerView: 'auto', freeMode: true, freeModeMomentumRatio: 0.5, freeModeMomentumBounce: false, freeModeMinimumVelocity: 0.1, grabCursor: true, mousewheel: true, resistanceRatio: 0.1 }) }) } ``` 通过设置 `freeMode: true` 启用 freeMode 模式,`grabCursor: true` 启用鼠标模式,`mousewheel: true` 启用鼠标滚轮控制模式等参数,可以实现鼠标轮播效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值