Vue3实现div拖拽改变宽高

效果图如下:

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

<template>
    <div id="business_plane">
        <div class="business_plane" ref="container">
            <div class="darg_tool">
                <el-icon class="drag_H" title="点击拖动调整高度" @mousedown="onTdMousedown($event)"
                    @mouseleave="onTdMouseleave($event)">
                    <DCaret />
                </el-icon>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { shallowRef, onMounted, nextTick, ref, reactive } from 'vue'
import { CircleClose,DCaret } from '@element-plus/icons-vue'

const domInfo = reactive({
    baseW: 0,
    baseH: 0,
    searchToolMT: 10,
});

const container = ref<HTMLElement>();

const updateTarget = (event: MouseEvent) => {
    if (!container.value) {
        console.error('drag--- 请传入一个HTMLElement节点');
        return;
    }
    // movementX/movementY
    // 两个鼠标移动事件间隔时间中当中鼠标移动的相对坐标;
    domInfo.baseW = container.value.clientWidth;
    domInfo.baseH = container.value.clientHeight;
    domInfo.searchToolMT = document.getElementById("searchTool")!.clientHeight - 10;
    //container.value!.style.width = `${domInfo.baseW + event.movementX}px`;
    if (parseInt(`${domInfo.baseH + event.movementY}`) < 550) {
        container.value!.style.height = '550px';
        document.getElementById("searchTool")!.style.marginTop = "10px"
        return
    }
    container.value!.style.height = `${domInfo.baseH + event.movementY}px`;
    document.getElementById("searchTool")!.style.marginTop = `${domInfo.searchToolMT + event.movementY}px`;
    document.getElementById("tree")!.style.height = `${domInfo.baseH + event.movementY - 110}px`;
};
// change 回调方式
const onTdMousedown = (e: MouseEvent) => {
    window.addEventListener('mousemove', updateTarget);
    window.onmouseup = function () {
        window.onmouseup = null;
        window.removeEventListener('mousemove', updateTarget);
    };
};

const onTdMouseleave = (e: MouseEvent) => {
    window.removeEventListener('mousemove', updateTarget);
}

</script>

<style lang="less" scoped>
#business_plane {
    position: absolute;
    top: 80px;
    right: 35px;
    z-index: 999;
}

.business_plane {
    position: relative;
    color: #fff;
    width: 300px;
    height: 550px;
    background-image: url(../../assets/images/modal_bg1.png);
    background-size: 100% 100%;
}

.darg_tool {
    width: 300px;
    height: 50px;
    display: flex;
    justify-content: center;
}

.drag_H {
    position: absolute;
    width: 30px;
    height: 30px;
    bottom: 0px;
    font-size: 20px;
    z-index: 999;
    border-radius: inherit;
    cursor: move;
}

.container {
    width: 100px;
    height: 100px;
    background-color: #ccc;
    position: relative;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值