<template>
<div>
<div>
<div class="layoutJSON">
Displayed as <code>[x, y, w, h]</code>:
<div class="columns">
<div class="layoutItem" v-for="item in layout">
<b>{{ item.i }}</b>: [{{ item.x }}, {{ item.y }}, {{ item.w }}, {{ item.h }}]
</div>
</div>
</div>
</div>
<br/>
<div @drag="drag" @dragend="dragend" class="droppable-element" draggable="true"
unselectable="on">Droppable Element (Drag me!)</div>
<div id="content">
<grid-layout ref="gridlayout" :layout.sync="layout"
:col-num="12"
:row-height="30"
:is-draggable="true"
:is-resizable="true"
:vertical-compact="true"
:use-css-transforms="true"
>
<grid-item :key="item.i" v-for="item in layout"
:x="item.x"
:y="item.y"
:w="item.w"
:h="item.h"
:i="item.i"
>
<span class="text">{{ item.i }}</span>
</grid-item>
</grid-layout>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, unref, getCurrentInstance, defineExpose } from 'vue'
const ctx = getCurrentInstance().ctx
let mouseXY = { x: null, y: null }
let DragPos = { x: null, y: null, w: 1, h: 1, i: null }
let colNum = 10
const gridlayout = ref(null)
const gridItemRefs = ref([])
const getGridItemRef = (el, index) => {
if (el) {
gridItemRefs.value[index] = el
}
}
const layout = ref([
{ x: 0, y: 0, w: 10, h: 1, i: '0' },
{ x: 0, y: 0, w: 10, h: 2, i: '1' },
{ x: 0, y: 0, w: 2, h: 3, i: '2' },
{ x: 0, y: 0, w: 2, h: 4, i: '3' },
{ x: 0, y: 0, w: 2, h: 5, i: '4' },
{ x: 0, y: 0, w: 2, h: 6, i: '5' },
])
onMounted(() => {
document.addEventListener(
'dragover',
function (e) {
mouseXY.x = e.clientX
mouseXY.y = e.clientY
},
false,
)
})
function removeItem(val) {
const layoutUnref = unref(layout)
const index = layoutUnref.map((item) => item.i).indexOf(val)
this.layout.splice(index, 1)
}
function clickGridItem(item) {
console.log(item)
}
function drag() {
const layoutUnref = unref(layout)
let parentRect = document.getElementById('content').getBoundingClientRect()
let mouseInGrid = false
if (
mouseXY.x > parentRect.left &&
mouseXY.x < parentRect.right &&
mouseXY.y > parentRect.top &&
mouseXY.y < parentRect.bottom
) {
mouseInGrid = true
}
if (mouseInGrid === true && layoutUnref.findIndex((item) => item.i === 'drop') === -1) {
layoutUnref.push({
x: (layoutUnref.length * 2) % (colNum || 12),
y: layoutUnref.length + (colNum || 12), // puts it at the bottom
w: 1,
h: 1,
i: 'drop',
})
}
let index = layoutUnref.findIndex((item) => item.i === 'drop')
if (index !== -1) {
try {
gridItemRefs.value[layoutUnref.length].style.display = 'none'
} catch {}
let el = gridItemRefs.value[index]
if (mouseInGrid === true && el) {
el.dragging = { top: mouseXY.y - parentRect.top, left: mouseXY.x - parentRect.left }
let new_pos = el.calcXY(mouseXY.y - parentRect.top, mouseXY.x - parentRect.left)
gridlayout.value.dragEvent('dragstart', 'drop', new_pos.x, new_pos.y, 1, 1)
DragPos.i = String(index)
DragPos.x = layoutUnref[index].x
DragPos.y = layoutUnref[index].y
}
if (mouseInGrid === false) {
gridlayout.value.dragEvent('dragend', 'drop', 1, 1, 1, 1)
layout.value = layoutUnref.filter((obj) => obj.i !== 'drop')
}
}
}
function dragend() {
const layoutUnref = unref(layout)
let parentRect = document.getElementById('content').getBoundingClientRect()
let mouseInGrid = false
if (
mouseXY.x > parentRect.left &&
mouseXY.x < parentRect.right &&
mouseXY.y > parentRect.top &&
mouseXY.y < parentRect.bottom
) {
mouseInGrid = true
}
if (mouseInGrid === true) {
gridlayout.value.dragEvent('dragend', 'drop', DragPos.x, DragPos.y, 1, 1)
layout.value = layoutUnref.filter((obj) => obj.i !== 'drop')
layout.value.push({
x: DragPos.x,
y: DragPos.y,
w: componentInfo?.w ?? 1,
h: componentInfo?.h ?? 1,
i: DragPos.i,
})
console.log(layout.value)
// gridlayout.value.dragEvent('dragend', DragPos.i, DragPos.x, DragPos.y, 1, 1)
// try {
// gridItemRefs.value[layoutUnref.length].style.display = 'block'
// } catch {}
}
}
defineExpose({
drag,
dragend,
})
</script>
<style scoped>
.droppable-element {
width: 150px;
text-align: center;
background: #fdd;
border: 1px solid black;
margin: 10px 0;
padding: 10px;
}
.vue-grid-layout {
background: #eee;
}
.vue-grid-item:not(.vue-grid-placeholder) {
background: #ccc;
border: 1px solid black;
}
.vue-grid-item .resizing {
opacity: 0.9;
}
.vue-grid-item .static {
background: #cce;
}
.vue-grid-item .text {
font-size: 24px;
text-align: center;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
height: 100%;
width: 100%;
}
.vue-grid-item .no-drag {
height: 100%;
width: 100%;
}
.vue-grid-item .minMax {
font-size: 12px;
}
.vue-grid-item .add {
cursor: pointer;
}
.vue-draggable-handle {
position: absolute;
width: 20px;
height: 20px;
top: 0;
left: 0;
background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><circle cx='5' cy='5' r='5' fill='#999999'/></svg>") no-repeat;
background-position: bottom right;
padding: 0 8px 8px 0;
background-repeat: no-repeat;
background-origin: content-box;
box-sizing: border-box;
cursor: pointer;
}
.layoutJSON {
background: #ddd;
border: 1px solid black;
margin-top: 10px;
padding: 10px;
}
.layoutJSON {
background: #ddd;
border: 1px solid black;
margin-top: 10px;
padding: 10px;
}
.columns {
-moz-columns: 120px;
-webkit-columns: 120px;
columns: 120px;
}
</style>
vue-grid-layout組件用vue3寫外部元素拖入功能
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來先较,“玉大人,你說我怎么就攤上這事闲勺≡兀” “怎么了菜循?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長癌幕。 經(jīng)常有香客問我衙耕,道長勺远,這世上最難降的妖魔是什么橙喘? 我笑而不...
- 正文 為了忘掉前任胶逢,我火速辦了婚禮,結(jié)果婚禮上初坠,老公的妹妹穿的比我還像新娘。我一直安慰自己碟刺,他們只是感情好比搭,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著身诺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪霉赡。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼氢橙!你這毒婦竟也來了酝枢?” 一聲冷哼從身側(cè)響起悍手,我...
- 正文 年R本政府宣布榆鼠,位于F島的核電站,受9級特大地震影響妆够,放射性物質(zhì)發(fā)生泄漏识啦。R本人自食惡果不足惜神妹,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望鸵荠。 院中可真熱鬧冕茅,春花似錦腰鬼、人聲如沸嵌赠。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽彼硫。三九已至炊豪,卻和暖如春拧篮,著一層夾襖步出監(jiān)牢的瞬間词渤,已是汗流浹背串绩。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- vue-grid-layout官網(wǎng)案例10是拖拽后彈出坐標信息箍铲,我把它放在容器內(nèi)去了鬓椭,改變第一個被拖拽進去的ite...
- 3、組件和元素切換動畫的實現(xiàn) 代碼 運行結(jié)果 使得它們”出入有序“ mode="out-in" 運行結(jié)果 第一次渲...
- 2小染、使用 transition 標簽實現(xiàn)單元素組件的過渡和動畫效果 出入場動畫 運行結(jié)果 出入場動畫 運行結(jié)果 給...
- 前言 首先為什么不直接使用css的hover呵扛?因為有多個地方都有這個筐带,每個元素都包裹一個彈窗可以是可以今穿,但是dom...
- 一伦籍、 $ref 在Vue中想直接獲取元素對象或子組件的實例的時候蓝晒,可以給元素或者組件綁定一個ref 的 attri...