vue3實(shí)現(xiàn)手機(jī)上可拖拽元素的組件
前言:
用vue3實(shí)現(xiàn)一個(gè)可在手機(jī)上拖拽元素的組件,可拖拽至任意位置,并且可以防止拖拽元素移出屏幕邊緣。
<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實(shí)現(xiàn)手機(jī)上可拖拽元素的組件的文章就介紹到這了,更多相關(guān)vue3可拖拽元素組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ElementUI?復(fù)雜頂部和左側(cè)導(dǎo)航欄實(shí)現(xiàn)示例
本文主要介紹了ElementUI?復(fù)雜頂部和左側(cè)導(dǎo)航欄實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)
這篇文章主要介紹了Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04如何優(yōu)雅的在一臺(tái)vps(云主機(jī))上面部署vue+mongodb+express項(xiàng)目
這篇文章主要介紹了如何優(yōu)雅的在一臺(tái)vps(云主機(jī))上面部署vue+mongodb+express項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-01-01通過cordova將vue項(xiàng)目打包為webapp的方法
這篇文章主要介紹了通過cordova將vue項(xiàng)目打包為webapp的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-02-02vue實(shí)現(xiàn)循環(huán)滾動(dòng)列表
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)循環(huán)滾動(dòng)列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06