vue.js?自定義指令(拖拽、拖動、移動)?指令?v-drag詳解
更新時間:2023年01月23日 10:31:14 作者:丿Mr_Liu
這篇文章主要介紹了vue.js?自定義指令(拖拽、拖動、移動)?指令?v-drag,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
1.main.js文件中添加已下代碼
Vue.directive('drag', {
//1.指令綁定到元素上回立刻執(zhí)行bind函數(shù),只執(zhí)行一次
//2.每個函數(shù)中第一個參數(shù)永遠(yuǎn)是el,表示綁定指令的元素,el參數(shù)是原生js對象
//3.通過el.focus()是無法獲取焦點的,因為只有插入DOM后才生效
bind: function (el) { },
//inserted表示一個元素,插入到DOM中會執(zhí)行inserted函數(shù),只觸發(fā)一次
inserted: function (el) {
let odiv = el; //獲取當(dāng)前元素
let firstTime = '', lastTime = '';
el.onmousedown = function (e) {
var disx = e.pageX - el.offsetLeft;
var disy = e.pageY - el.offsetTop;
// 給當(dāng)前元素添加屬性,用于元素狀態(tài)的判斷
odiv.setAttribute('ele-flag', false)
odiv.setAttribute('draging-flag', true)
firstTime = new Date().getTime();
document.onmousemove = function (e) {
el.style.left = e.pageX - disx + 'px';
el.style.top = e.pageY - disy + 'px';
}
document.onmouseup = function (event) {
document.onmousemove = document.onmouseup = null;
lastTime = new Date().getTime();
if ((lastTime - firstTime) > 200) {
odiv.setAttribute('ele-flag', true)
event.stopPropagation()
}
setTimeout(function () {
odiv.setAttribute('draging-flag', false)
}, 100)
}
}
}
})
2.組件中的使用
<template>
<div class="drag" v-drag @click="handleDragClick"> 我是拖拽的div<div>
<template>
<script>
methods:{
handleDragClick(e){
// 判斷拖拽組件的狀態(tài)
let isDrag = false;
try {
isDrag = e.target.getAttribute('ele-flag') === 'true';
}catch (e) {
}
if(isDrag){
return;
}
// 當(dāng)推拽組件未在 拖動狀態(tài) 執(zhí)行點擊事件
// todo 下面是執(zhí)行點擊事件的代碼
}
}
</script>
<style>
// 這里是拖拽 組件的樣式
.drag{
width:100px;
height:100px;
border:1px solid pink;
}
</style>補充:vue自定義拖拽指令v-drag
<template>
<div class="drag" v-drag ref="drag"></div>
</template>
<script>
export default {
name: 'Home',
data(){
return{
positionX:'',
positionY:''
}
},
mounted () {
this.$refs.drag.style.top = window.localStorage.getItem('top')+'px'
this.$refs.drag.style.left = window.localStorage.getItem('left')+'px'
},
directives: {
drag: {
// 指令的定義
bind: function (el,binding,vnode) {
console.log(el);
console.log(binding);
console.log(vnode.context);
let odiv = el; //獲取當(dāng)前元素
odiv.onmousedown = (e) => {
//算出鼠標(biāo)相對元素的位置
let disX = e.clientX - odiv.offsetLeft;
let disY = e.clientY - odiv.offsetTop;
document.onmousemove = (e)=>{
//用鼠標(biāo)的位置減去鼠標(biāo)相對元素的位置,得到元素的位置
let left = e.clientX - disX;
let top = e.clientY - disY;
//綁定元素位置到positionX和positionY上面
vnode.context.positionX = top;
vnode.context.positionY = left;
window.localStorage.setItem('top',top)
window.localStorage.setItem('left',left)
//移動當(dāng)前元素
odiv.style.left = left + 'px';
odiv.style.top = top + 'px';
};
document.onmouseup = () => {
document.onmousemove = null;
document.onmouseup = null;
};
};
}
}
}
}
</script>
<style lang="scss" scoped>
.drag{
position: relative; /*定位*/
// top: 10px;
// left: 10px;
width: 200px;
height: 200px;
background: #666; /*設(shè)置一下背景*/
}
</style>到此這篇關(guān)于vue.js 自定義指令(拖拽、拖動、移動) 指令 v-drag的文章就介紹到這了,更多相關(guān)vue.js 自定義指令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+Echart柱狀圖實現(xiàn)疫情數(shù)據(jù)統(tǒng)計
這篇文章主要介紹了在Vue nuxt項目中,如何使用Echart(百度圖表)柱狀圖來實現(xiàn)疫情數(shù)據(jù)統(tǒng)計,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12
vue和react等項目中更簡單的實現(xiàn)展開收起更多等效果示例
這篇文章主要介紹了vue和react等項目中更簡單的實現(xiàn)展開收起更多等效果示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02
Vue實現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件詳解
在所做的Vue項目中,有時候需要在鼠標(biāo)移動文字框的時候顯示一些詳細(xì)信息,下面這篇文章主要給大家介紹了關(guān)于Vue實現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件的相關(guān)資料,需要的朋友可以參考下2022-11-11

