首页 > 其他分享 >拖拽功能 vue + 移动端

拖拽功能 vue + 移动端

时间:2022-11-07 18:12:16浏览次数:49  
标签:box vue ny event nx item touch 移动 拖拽

1.   在components里面创建一个公共组件,dragMove.vue

<!-- 拖拽滑动效果 -->
<template>
<div id="item_box" @click="goMove" @touchstart="down" @touchmove="move"
@touchend="end">
//这里注意,我使用了阿里图标,你们使用的话,删除掉写自己的样式就好
<i class="iconfont icon-a-bianlidian2x"></i>
</div>
</template>

<script>
export default {
name: "defaultDrag",
data() {
return {
flags: false,
position: { x: 0, y: 0 },
nx: "",
ny: "",
dx: "",
dy: "",
xPum: "",
yPum: "",
};
},
methods: {
goNext() {
this.$emit("goMove");
},
// 实现移动端拖拽
down() {
let item_box = document.querySelector("#item_box");
this.flags = true;
var touch;
if (event.touches) {
touch = event.touches[0];
} else {
touch = event;
}
this.maxW = document.body.clientWidth - item_box.offsetWidth;
this.maxH = document.body.clientHeight - item_box.offsetHeight;

this.position.x = touch.clientX - item_box.offsetLeft;
this.position.y = touch.clientY - item_box.offsetTop;
this.dx = touch.clientX;
this.dy = touch.clientY;
},
move(event) {
event.preventDefault();
let item_box = document.querySelector("#item_box");
if (this.flags) {
var touch;
if (event.touches) {
touch = event.touches[0];
} else {
touch = event;
}
this.nx = touch.clientX - this.position.x;
this.ny = touch.clientY - this.position.y;

if (this.nx < 0) {
this.nx = 0;
} else if (this.nx > this.maxW) {
this.nx = this.maxW;
}

if (this.ny < 0) {
this.ny = 0;
} else if (this.ny >= this.maxH) {
this.ny = this.maxH;
}

item_box.style.left = this.nx + "px";
item_box.style.top = this.ny + "px";
document.addEventListener(
"touchmove",
function() {
// event.preventDefault()
},
false
);
}
},
//鼠标释放
end() {
this.flags = false;
},
},
};
</script>


<style lang="scss" scoped>
#item_box {
width: 1rem;
height: 1rem;
position: fixed;
z-index: 1000;
bottom: 60px;
right: 0.4rem;
border-radius: 50%;
border: 2px solid #ffffff;
box-shadow: 0 0 0.4rem 2px skyblue;
background: #81f8e8;
display: flex;
justify-content: center;
align-items: center;
.iconfont {
color: orange;
}
}
</style>

 

 

 

 

 

2.  然后在你们需要使用拖拽的组件中引入 dragMove.vue子组件  就好  

(不要忘记在components中注册子组件哟)

//:goMove是子传夫的事件,不用执行任何步骤,写到methods中就好
<dragMove @goMove="watchCar"></dragMove>

 

标签:box,vue,ny,event,nx,item,touch,移动,拖拽
From: https://www.cnblogs.com/dreammiao/p/16866913.html

相关文章

  • vue2.0引入css文件后报错
    vue2.0的main.js中引入css文件后报错。报错示例:UncaughtError:Moduleparsefailed:/Users/**/Desktop/vue2/node_modules/.1.0.0-rc.5@element-ui/lib/theme-default/i......
  • vue项目中定义全局变量、全局函数
    在vue项目中,我们经常会遇到要在多个页面组件中都要使用的一个变量或者函数,比如说需要配置的ip地址,在生产环境和测试环境不一样就需要修改。因此我们可以定义一个全局变量来......
  • Vue构建一个项目
    vue2.0推荐开发环境Homebrew1.0.6(Mac)Node.js6.7.0npm3.10.3webpack1.13.2vue-cli2.4.0Atom1.10.2构建vue项目1、下载node.js下载地址:https://nodejs.org/en/检查是......
  • Vue 3.x 的 script setup 语法糖用法详解
    由于原来vue3中的setupCompositionAPI语法太过于冗长麻烦,官方又出了这么个语法糖,非常的好用了。这里介绍一些常用的语法:一、如何开始使用?1、需要关闭vetur插件,安装Vol......
  • vue组件间的通讯(10种方法)【重要】(待补充。。。)
    1.props/$emitprops主要用于父组件传递数据给子组件,父==>子。Vue自定义事件父组件可以在使用子组件的地方直接用v-on来监听子组件触发的事件。即父组件中使用v-on绑定自......
  • 【前端开发】如何将vue组件转成自定义指令使用,(在自定义指令中使用element ui组件或常
    //导入组件importVuefrom'vue'importXxTipsfrom'packages/basic/xx-tips/src/XxTips.vue'//自定义指令Vue.directive('tip',{bind(el,binding){el.......
  • Vue-Router
    Vue-Router目录Vue-Router1.路由的安装与基本使用2.带参的动态路由3.页面导航4.关于路由的命名5.关于路由传参null6.路由导航守卫7.动态路由1.路由的安装与基本使用......
  • Vue中做table分页
    <template> <divclass="wrap">  <!--<el-card>-->   <el-table:data="tableData"borderstripestyle="width:100%">    <el-table-column......
  • vue-计算属性和监视属性的区别和使用方法
    转载于:https://blog.csdn.net/qq_38110274/article/details/121242203 作者:我是天之涯  一、总述computed和watch都是vue框架中的用于监听数据变化的属性。 二......
  • vuecli的项目结构
    src中为我们需要编写的资源assets中为公共资源如图片多媒体文件等components中为我们编写的子组件app为子组件入口mainjs为总入口文件......