• Vue3实现div拖拽改变宽高


    效果图如下:

    底部拖拽按钮点击拖拽可自定义父容器的宽高

    1. <template>
    2. <div id="business_plane">
    3. <div class="business_plane" ref="container">
    4. <div class="darg_tool">
    5. <el-icon class="drag_H" title="点击拖动调整高度" @mousedown="onTdMousedown($event)"
    6. @mouseleave="onTdMouseleave($event)">
    7. <DCaret />
    8. el-icon>
    9. div>
    10. div>
    11. div>
    12. template>
    13. <script lang="ts" setup>
    14. import { shallowRef, onMounted, nextTick, ref, reactive } from 'vue'
    15. import { CircleClose,DCaret } from '@element-plus/icons-vue'
    16. const domInfo = reactive({
    17. baseW: 0,
    18. baseH: 0,
    19. searchToolMT: 10,
    20. });
    21. const container = ref<HTMLElement>();
    22. const updateTarget = (event: MouseEvent) => {
    23. if (!container.value) {
    24. console.error('drag--- 请传入一个HTMLElement节点');
    25. return;
    26. }
    27. // movementX/movementY
    28. // 两个鼠标移动事件间隔时间中当中鼠标移动的相对坐标;
    29. domInfo.baseW = container.value.clientWidth;
    30. domInfo.baseH = container.value.clientHeight;
    31. domInfo.searchToolMT = document.getElementById("searchTool")!.clientHeight - 10;
    32. //container.value!.style.width = `${domInfo.baseW + event.movementX}px`;
    33. if (parseInt(`${domInfo.baseH + event.movementY}`) < 550) {
    34. container.value!.style.height = '550px';
    35. document.getElementById("searchTool")!.style.marginTop = "10px"
    36. return
    37. }
    38. container.value!.style.height = `${domInfo.baseH + event.movementY}px`;
    39. document.getElementById("searchTool")!.style.marginTop = `${domInfo.searchToolMT + event.movementY}px`;
    40. document.getElementById("tree")!.style.height = `${domInfo.baseH + event.movementY - 110}px`;
    41. };
    42. // change 回调方式
    43. const onTdMousedown = (e: MouseEvent) => {
    44. window.addEventListener('mousemove', updateTarget);
    45. window.onmouseup = function () {
    46. window.onmouseup = null;
    47. window.removeEventListener('mousemove', updateTarget);
    48. };
    49. };
    50. const onTdMouseleave = (e: MouseEvent) => {
    51. window.removeEventListener('mousemove', updateTarget);
    52. }
    53. script>
    54. <style lang="less" scoped>
    55. #business_plane {
    56. position: absolute;
    57. top: 80px;
    58. right: 35px;
    59. z-index: 999;
    60. }
    61. .business_plane {
    62. position: relative;
    63. color: #fff;
    64. width: 300px;
    65. height: 550px;
    66. background-image: url(../../assets/images/modal_bg1.png);
    67. background-size: 100% 100%;
    68. }
    69. .darg_tool {
    70. width: 300px;
    71. height: 50px;
    72. display: flex;
    73. justify-content: center;
    74. }
    75. .drag_H {
    76. position: absolute;
    77. width: 30px;
    78. height: 30px;
    79. bottom: 0px;
    80. font-size: 20px;
    81. z-index: 999;
    82. border-radius: inherit;
    83. cursor: move;
    84. }
    85. .container {
    86. width: 100px;
    87. height: 100px;
    88. background-color: #ccc;
    89. position: relative;
    90. }
    91. style>

  • 相关阅读:
    3款堪称神器的电脑软件,简单又实用
    FPN模型
    苹果春季发布会前瞻:全新 iPhone SE 3 将揭晓,M2芯片来袭?
    C# OpenVino Yolov8 Pose 姿态识别
    nvm管理node版本过程记录
    时间序列(一):日期和时间处理
    es6 语法,在个别浏览器中不兼容的处理办法
    STM32 HAL库DMA串口发送数据参考文章
    /etc/sudoers文件未配置nopasswd:但sudo su没有输密码就直接进root了
    Vim实用技巧_8.替换(substitute)和global命令
  • 原文地址:https://blog.csdn.net/czj_com/article/details/133686190