vue怎么实现拖拽窗口功能(vue,开发技术)

时间:2024-05-03 11:52:10 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

效果

vue怎么实现拖拽窗口功能

实现代码

<template><transition><!--绑定style中top和left--><divclass="moveBox":v-show="show"><divclass="moveHead"@mousedown="mousedown"@mousemove="mousemove"@mouseup="mouseup"@mouseleave="mousemove"></div><!--关闭按钮--><divclass="close"@click="toggleShow">×</div><divclass="content"><!--插槽--><slot></slot></div></div></transition></template><script>exportdefault{name:"moveBox",data(){return{show:true,//是否显示x:100,//left:xy:50,//top:yleftOffset:0,//鼠标距离移动窗口左侧偏移量topOffset:0,//鼠标距离移动窗口顶部偏移量isMove:false,//是否移动标识};},computed:{//top与left加上pxposition(){return`top:${this.y}px;left:${this.x}px;`;},},methods:{//控制打开关闭toggleShow(){this.x=100;this.y=50;this.show=!this.show;},mousedown(event){//鼠标按下事件this.leftOffset=event.offsetX;this.topOffset=event.offsetY;this.isMove=true;},//鼠标移动mousemove(event){if(!this.isMove){return;}this.x=event.clientX-this.leftOffset;this.y=event.clientY-this.topOffset;},//鼠标抬起mouseup(){this.isMove=false;},},};</script><stylelang="less"scoped>.moveBox{width:500px;height:300px;position:fixed;box-shadow:005px3px#95a5a6;.moveHead{height:30px;background-color:#bdc3c7;cursor:move;}.close{position:absolute;right:0;top:0;line-height:30px;font-size:24px;cursor:pointer;display:block;width:30px;height:30px;text-align:center;}}.v-enter,.v-leave-to{opacity:0;transform:scale(0.5);}.content{padding:10px;}.v-enter-active,.v-leave-active{transition:all0.5sease;}</style>

使用

<template><divclass="home"><button@click="$refs.moveBox.toggleShow()">togglemoveBox</button><move-boxref="moveBox">HelloWorld</move-box></div></template>

代码没什么难度,主要是使用了定位,在鼠标移动事件中定义top和left值。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:vue怎么实现拖拽窗口功能的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Java的贪心和枚举怎么使用下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18