vue3實現(xiàn)手機上可拖拽元素的組件
前言:
用vue3實現(xiàn)一個可在手機上拖拽元素的組件,可拖拽至任意位置,并且可以防止拖拽元素移出屏幕邊緣。
<script setup>
import { ref } from "vue";
const props = defineProps({
disabled: { type: Boolean, default: false }
});
const dragPos = {
hasMoved: false, // 排除click事件
x: 0, // right
y: 0, // bottom
startX: 0,
startY: 0,
endX: 0,
endY: 0
};
const dragBoxPos = ref({ x: null, y: null });
const dragBoxRef = ref();
const setPosition = (dragX, dragY) => {
[dragX, dragY] = _getSafeAreaXY(dragX, dragY);
dragPos.x = dragX;
dragPos.y = dragY;
dragBoxPos.value.x = dragX;
dragBoxPos.value.y = dragY;
};
const _getSafeAreaXY = (x, y) => {
const docWidth = Math.max(
document.documentElement.offsetWidth,
window.innerWidth
);
const docHeight = Math.max(
document.documentElement.offsetHeight,
window.innerHeight
);
// 檢查屏幕邊緣
if (x + dragBoxRef.value.offsetWidth > docWidth) {
x = docWidth - dragBoxRef.value.offsetWidth;
}
if (y + dragBoxRef.value.offsetHeight > docHeight) {
y = docHeight - dragBoxRef.value.offsetHeight;
}
if (x < 0) {
x = 0;
}
// iOS底部的安全區(qū)域
if (y < 20) {
y = 20;
}
return [x, y];
};
const onTouchStart = (e) => {
if (props.disabled) return;
dragPos.startX = e.touches[0].pageX;
dragPos.startY = e.touches[0].pageY;
dragPos.hasMoved = false;
};
const onTouchEnd = (e) => {
if (props.disabled) return;
if (!dragPos.hasMoved) return;
dragPos.startX = 0;
dragPos.startY = 0;
dragPos.hasMoved = false;
setPosition(dragPos.endX, dragPos.endY);
};
const onTouchMove = (e) => {
if (props.disabled) return;
if (e.touches.length <= 0) return;
const offsetX = e.touches[0].pageX - dragPos.startX,
offsetY = e.touches[0].pageY - dragPos.startY;
let x = Math.floor(dragPos.x - offsetX),
y = Math.floor(dragPos.y - offsetY);
[x, y] = _getSafeAreaXY(x, y);
dragBoxPos.value.x = x;
dragBoxPos.value.y = y;
dragPos.endX = x;
dragPos.endY = y;
dragPos.hasMoved = true;
e.preventDefault();
};
</script>
<template>
<div
ref="dragBoxRef"
class="tvb-drag-box"
:class="{ disabled: disabled }"
:style="
disabled ? '' : `right: ${dragBoxPos.x}px; bottom: ${dragBoxPos.y}px;`
"
@touchstart="onTouchStart"
@touchend="onTouchEnd"
@touchmove="onTouchMove"
>
<slot></slot>
</div>
</template>
<style lang="scss" scoped>
.tvb-drag-box {
&:not(.disabled) {
position: fixed;
bottom: 10px;
right: 10px;
overflow: hidden;
z-index: 99;
}
}
</style>使用方式:
<drag-box>
<button>我是內(nèi)容</button>
</drag-box>到此這篇關(guān)于vue3實現(xiàn)手機上可拖拽元素的組件的文章就介紹到這了,更多相關(guān)vue3可拖拽元素組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue無法加載文件C:\xx\AppData\Roaming\npm\vue.ps1系統(tǒng)禁止運行腳本
這篇文章主要介紹了vue?:?無法加載文件?C:\xx\AppData\Roaming\npm\vue.ps1...系統(tǒng)上禁止運行腳本問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Vue Element使用icon圖標(biāo)教程詳解(第三方)
element-ui自帶的圖標(biāo)庫不夠全,還是需要需要引入第三方icon。下面小編給大家?guī)砹薞ue Element使用icon圖標(biāo)教程,感興趣的朋友一起看看吧2018-02-02
Vue中ElementUI結(jié)合transform使用時彈框定位不準(zhǔn)確問題解析
在近期開發(fā)中,需要將1920*1080放到更大像素大屏上演示,所以需要使用到transform來對頁面進(jìn)行縮放,但是此時發(fā)現(xiàn)彈框定位出錯問題,無法準(zhǔn)備定位到實際位置,本文給大家分享Vue中ElementUI結(jié)合transform使用時彈框定位不準(zhǔn)確解決方法,感興趣的朋友一起看看吧2024-01-01
Vue中使用video.js實現(xiàn)截圖和視頻錄制與下載
這篇文章主要為大家詳細(xì)介紹了Vue中如何使用video.js實現(xiàn)截圖和視頻錄制與下載,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Laravel 如何在blade文件中使用Vue組件的示例代碼
這篇文章主要介紹了Laravel 如何在blade文件中使用Vue組件,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06

