vue-drag-resize與輸入框/文本框沖突問題
vue-drag-resize與輸入框/文本框沖突
拖拽是前端使用較為頻繁的功能了,當(dāng)我們使用vue-drag-resize插件進(jìn)行拖拽功能實(shí)現(xiàn)時(shí),發(fā)現(xiàn)它和輸入框或文本框有沖突,輸入框/文本框無法輸入。
今天主要說怎么去解決該問題。
在測試過程中,發(fā)現(xiàn)出現(xiàn)該問題的原因是輸入框的焦點(diǎn)事件和拖拽的點(diǎn)擊事件沖突了。
找到原因我們就能去解決。
vue-drag-resize插件文檔中提供的解決辦法
<vue-drag-resize @activated="activateEv(index)" />
activateEv(index) {
?? ?console.log('activateEv' + index);
?? ?this.$refs['drag-input'].focus();
}插件提供的方法確實(shí)可以解決該問題,但是在之后的開發(fā)中發(fā)現(xiàn),這針對(duì)單個(gè)輸入框或文本框確實(shí)有效,但是**如果一個(gè)彈窗內(nèi)存在多個(gè)輸入框/文本框時(shí),只有最后一個(gè)輸入框/文本框有效**,說明問題還是沒有得到解決。
解決多輸入框/文本框沖突
思路
其實(shí)我們看插件提供的方法,就是給輸入框/文本框主動(dòng)的設(shè)置焦點(diǎn)事件。那如果我們點(diǎn)擊哪個(gè)輸入框/文本框時(shí)才給哪個(gè)設(shè)置焦點(diǎn)事件不就可以解決問題嗎。
針對(duì)這個(gè)思路,我們進(jìn)行代碼調(diào)整。
<detailmove @clicked="clickHandle">
clickHandle(e){
? ? e.target.focus()
}clicked是插件自帶的點(diǎn)擊事件,當(dāng)我們點(diǎn)擊時(shí),獲取當(dāng)前點(diǎn)擊的dom元素,然后設(shè)置焦點(diǎn)事件。這樣就可以解決了。
當(dāng)然我們針對(duì)的是輸入框和文本框,那我們可以加判斷去區(qū)分。
<detailmove @clicked="clickHandle">
clickHandle(e){
? ?if (e.target.nodeName === 'INPUT' || e.target.nodeName === 'TEXTAREA') {
? ? ?? ?e.target.focus()
? ?}?
}這樣問題就解決了
vue-drag-resize拖拽組件的簡單使用
vue3
npm i -s vue-drag-resize@next
?
//局部使用
<template>
? <div class="home">
? ? <VueDragResize
? ? ? class="list"
? ? ? :isActive="true"
? ? ? :w="width"
? ? ? :h="height"
? ? ? ? ?:x="left"
? ? ? :y="top"
? ? ? :parentLimitation="parentLimitation"
? ? ? :aspectRatio="aspectRatio"
? ? ? v-on:resizing="resize"
? ? ? v-on:dragging="resize"
? ? >
? ? ? <p>{{ top }} х {{ left }}</p>
? ? ? <p>{{ width }} х {{ height }}</p>
? ? </VueDragResize>
? </div>
</template>
?
<script>
// @ is an alias to /src
import VueDragResize from "vue-drag-resize";
export default {
? components: {
? ? VueDragResize,
? },
? name: "HomeView",
? data() {
? ? return {
? ? ? parentLimitation: true, //true不能超過父組件 fallse可以超過父組件
? ? ? aspectRatio: true, //false不限制寬高比例 true限制寬高比例等比縮放
? ? ? width: 100,
? ? ? height: 100,
? ? ? top: 0,
? ? ? left: 0,
? ? };
? },
? methods: {
? ? resize(newRect) {
? ? ? console.log(newRect);
? ? ? this.width = newRect.width;
? ? ? this.height = newRect.height;
? ? ? this.top = newRect.top;
? ? ? this.left = newRect.left;
? ? },
? },
};
</script>
<style lang="scss" scoped>
.home {
? width: 1920px;
? height: 1080px;
? position: relative;
? top: 0;
? left: 0;
? .list {
? ? position: absolute;
? ? top: 0;
? ? left: 0;
? }
}
</style>vue2
npm i -s vue-drag-resize
//局部使用
<template>
? <div class="home">
? ? <VueDragResize
? ? ? class="list"
? ? ? :isActive="true"
? ? ? :w="width"
? ? ? :h="height"
? ? ? ? ?:x="left"
? ? ? :y="top"
? ? ? :parentLimitation="parentLimitation"
? ? ? :aspectRatio="aspectRatio"
? ? ? v-on:resizing="resize"
? ? ? v-on:dragging="resize"
? ? >
? ? ? <p>{{ top }} х {{ left }}</p>
? ? ? <p>{{ width }} х {{ height }}</p>
? ? </VueDragResize>
? </div>
</template>
?
<script>
// @ is an alias to /src
import VueDragResize from "vue-drag-resize";
export default {
? components: {
? ? VueDragResize,
? },
? name: "HomeView",
? data() {
? ? return {
? ? ? parentLimitation: true, //true不能超過父組件 fallse可以超過父組件
? ? ? aspectRatio: true, //false不限制寬高比例 true限制寬高比例等比縮放
? ? ? width: 100,
? ? ? height: 100,
? ? ? top: 0,
? ? ? left: 0,
? ? };
? },
? methods: {
? ? resize(newRect) {
? ? ? console.log(newRect);
? ? ? this.width = newRect.width;
? ? ? this.height = newRect.height;
? ? ? this.top = newRect.top;
? ? ? this.left = newRect.left;
? ? },
? },
};
</script>
<style lang="scss" scoped>
.home {
? width: 1920px;
? height: 1080px;
? position: relative;
? top: 0;
? left: 0;
? .list {
? ? position: absolute;
? ? top: 0;
? ? left: 0;
? }
}
</style>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中video標(biāo)簽如何實(shí)現(xiàn)不靜音自動(dòng)播放
最近在做大屏展示需要在一開始播放引導(dǎo)視頻,產(chǎn)生自動(dòng)播放需求,下面這篇文章主要給大家介紹了關(guān)于Vue中video標(biāo)簽如何實(shí)現(xiàn)不靜音自動(dòng)播放的相關(guān)資料,需要的朋友可以參考下2023-01-01
用 Vue.js 遞歸組件實(shí)現(xiàn)可折疊的樹形菜單(demo)
通過本文給您演示一下如何有效地使用遞歸組件,我將通過建立一個(gè)可擴(kuò)展/收縮的樹形菜單的來一步步進(jìn)行。下面通過本文給大家分享用 Vue.js 遞歸組件實(shí)現(xiàn)可折疊的樹形菜單,需要的朋友參考下吧2017-12-12
vue和H5 draggable實(shí)現(xiàn)拖拽并替換效果
這篇文章主要為大家詳細(xì)介紹了vue和H5 draggable實(shí)現(xiàn)拖拽并替換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
解決vue打包 npm run build-test突然不動(dòng)了的問題
這篇文章主要介紹了解決vue打包 npm run build-test突然不動(dòng)了的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue3實(shí)現(xiàn)刷新頁面局部內(nèi)容的示例代碼
本文主要介紹了Vue3實(shí)現(xiàn)刷新頁面局部內(nèi)容的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

