vue-drag-resize与输入框/文本框冲突问题怎么解决

前端开发   发布日期:2025年04月11日   浏览次数:254

这篇“vue-drag-resize与输入框/文本框冲突问题怎么解决”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“vue-drag-resize与输入框/文本框冲突问题怎么解决”文章吧。

    vue-drag-resize与输入框/文本框冲突

    拖拽是前端使用较为频繁的功能了,当我们使用vue-drag-resize插件进行拖拽功能实现时,发现它和输入框或文本框有冲突,输入框/文本框无法输入。

    今天主要说怎么去解决该问题。

    在测试过程中,发现出现该问题的原因是输入框的焦点事件和拖拽的点击事件冲突了。

    找到原因我们就能去解决。

    vue-drag-resize插件文档中提供的解决办法

    1. <vue-drag-resize @activated="activateEv(index)" />
    2. activateEv(index) {
    3. console.log('activateEv' + index);
    4. this.$refs['drag-input'].focus();
    5. }

    插件提供的方法确实可以解决该问题,但是在之后的开发中发现,这针对单个输入框或文本框确实有效,但是**如果一个弹窗内存在多个输入框/文本框时,只有最后一个输入框/文本框有效**,说明问题还是没有得到解决。

    解决多输入框/文本框冲突

    思路

    其实我们看插件提供的方法,就是给输入框/文本框主动的设置焦点事件。那如果我们点击哪个输入框/文本框时才给哪个设置焦点事件不就可以解决问题吗。

    针对这个思路,我们进行代码调整。

    1. <detailmove @clicked="clickHandle">
    2. clickHandle(e){
    3. e.target.focus()
    4. }

    clicked是插件自带的点击事件,当我们点击时,获取当前点击的dom元素,然后设置焦点事件。这样就可以解决了。

    当然我们针对的是输入框和文本框,那我们可以加判断去区分。

    1. <detailmove @clicked="clickHandle">
    2. clickHandle(e){
    3. if (e.target.nodeName === 'INPUT' || e.target.nodeName === 'TEXTAREA') {
    4. e.target.focus()
    5. }
    6. }

    这样问题就解决了

    vue-drag-resize拖拽组件的简单使用

    vue3

    1. npm i -s vue-drag-resize@next
    2. //局部使用
    3. <template>
    4. <div class="home">
    5. <VueDragResize
    6. class="list"
    7. :isActive="true"
    8. :w="width"
    9. :h="height"
    10. :x="left"
    11. :y="top"
    12. :parentLimitation="parentLimitation"
    13. :aspectRatio="aspectRatio"
    14. v-on:resizing="resize"
    15. v-on:dragging="resize"
    16. >
    17. <p>{{ top }} х {{ left }}</p>
    18. <p>{{ width }} х {{ height }}</p>
    19. </VueDragResize>
    20. </div>
    21. </template>
    22. <script>
    23. // @ is an alias to /src
    24. import VueDragResize from "vue-drag-resize";
    25. export default {
    26. components: {
    27. VueDragResize,
    28. },
    29. name: "HomeView",
    30. data() {
    31. return {
    32. parentLimitation: true, //true不能超过父组件 fallse可以超过父组件
    33. aspectRatio: true, //false不限制宽高比例 true限制宽高比例等比缩放
    34. width: 100,
    35. height: 100,
    36. top: 0,
    37. left: 0,
    38. };
    39. },
    40. methods: {
    41. resize(newRect) {
    42. console.log(newRect);
    43. this.width = newRect.width;
    44. this.height = newRect.height;
    45. this.top = newRect.top;
    46. this.left = newRect.left;
    47. },
    48. },
    49. };
    50. </script>
    51. <style lang="scss" scoped>
    52. .home {
    53. width: 1920px;
    54. height: 1080px;
    55. position: relative;
    56. top: 0;
    57. left: 0;
    58. .list {
    59. position: absolute;
    60. top: 0;
    61. left: 0;
    62. }
    63. }
    64. </style>

    vue2

    1. npm i -s vue-drag-resize
    2. //局部使用
    3. <template>
    4. <div class="home">
    5. <VueDragResize
    6. class="list"
    7. :isActive="true"
    8. :w="width"
    9. :h="height"
    10. :x="left"
    11. :y="top"
    12. :parentLimitation="parentLimitation"
    13. :aspectRatio="aspectRatio"
    14. v-on:resizing="resize"
    15. v-on:dragging="resize"
    16. >
    17. <p>{{ top }} х {{ left }}</p>
    18. <p>{{ width }} х {{ height }}</p>
    19. </VueDragResize>
    20. </div>
    21. </template>
    22. <script>
    23. // @ is an alias to /src
    24. import VueDragResize from "vue-drag-resize";
    25. export default {
    26. components: {
    27. VueDragResize,
    28. },
    29. name: "HomeView",
    30. data() {
    31. return {
    32. parentLimitation: true, //true不能超过父组件 fallse可以超过父组件
    33. aspectRatio: true, //false不限制宽高比例 true限制宽高比例等比缩放
    34. width: 100,
    35. height: 100,
    36. top: 0,
    37. left: 0,
    38. };
    39. },
    40. methods: {
    41. resize(newRect) {
    42. console.log(newRect);
    43. this.width = newRect.width;
    44. this.height = newRect.height;
    45. this.top = newRect.top;
    46. this.left = newRect.left;
    47. },
    48. },
    49. };
    50. </script>
    51. <style lang="scss" scoped>
    52. .home {
    53. width: 1920px;
    54. height: 1080px;
    55. position: relative;
    56. top: 0;
    57. left: 0;
    58. .list {
    59. position: absolute;
    60. top: 0;
    61. left: 0;
    62. }
    63. }
    64. </style>

    以上就是vue-drag-resize与输入框/文本框冲突问题怎么解决的详细内容,更多关于vue-drag-resize与输入框/文本框冲突问题怎么解决的资料请关注九品源码其它相关文章!