原生JavaScript实现矩形块大小任意缩放

最近写了一个原生JavaScript实现矩形块大小任意缩放的案例,感觉里面的东西比较的绕,这里分享源码给大家,一起学习一下.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>原生JavaScript实现矩形块大小任意缩放</title>
</head>
<style>
    :root {
        --bor--width: 5px;
        --bor--height: 5px;
        --lines--bgc: red;
        --dots--bgc: black;
        --box--bgc: pink;
    }
    * {
        margin: 0;
        padding: 0;
    }
    .box {
        position: relative;
        top: 200px;
        left: 500px;
        width: 500px;
        height: 300px;
        background: var(--box--bgc);
    }
    .box>div {
        position: absolute;
    }
    .box>div:nth-child(-n+4) {
        width: var(--bor--width);
        height: var(--bor--height);
        background-color: var(--dots--bgc);
    }
    .box>div:nth-child(n+5) {
        background: var(--lines--bgc);
    }
    .box>.dot1 {
        cursor: nw-resize;
        top: calc(var(--bor--height)*-1);
        left: calc(var(--bor--width)*-1);
    }
    .box>.dot2 {
        cursor: ne-resize;
        top: calc(var(--bor--height)*-1);
        right: calc(var(--bor--width)*-1);
    }
    .box>.dot3 {
        cursor: se-resize;
        bottom: calc(var(--bor--height)*-1);
        right: calc(var(--bor--width)*-1);
    }
    .box>.dot4 {
        cursor: sw-resize;
        bottom: calc(var(--bor--height)*-1);
        left: calc(var(--bor--width)*-1);
    }
    .box>.line1,
    .box>.line3 {
        width: 100%;
        height: var(--bor--height);
    }
    .box>.line1 {
        top: calc(var(--bor--height)*-1);
        cursor: n-resize;
    }
    .box>.line3 {
        bottom: calc(var(--bor--height)*-1);
        cursor: s-resize;
    }
    .box>.line2,
    .box>.line4 {
        width: var(--bor--width);
        height: 100%;
    }
    .box>.line2 {
        right: calc(var(--bor--width)*-1);
        cursor: e-resize;
    }
    .box>.line4 {
        left: calc(var(--bor--width)*-1);
        cursor: w-resize;
    }
</style>
<body>
    <div class="box">
        <div class="dot1"></div>
        <div class="dot2"></div>
        <div class="dot3"></div>
        <div class="dot4"></div>
        <div class="line1"></div>
        <div class="line2"></div>
        <div class="line3"></div>
        <div class="line4"></div>
    </div>
    <script>
        //获取相关元素节点
        let box = document.querySelector(".box");
        let oDiv = document.querySelectorAll(".box>div")
        // 类边框宽度
        let bd = 5;
        //for循环指定事件对象
        for (let i = 0; i < 8; i++) {
            //矩形顶点点击拖动调整大小
            oDiv[i].onmousedown = function (ev) {
                //点击事件初始化相关值
                let oldY = ev.clientY;      //顶点原本的y值
                let oldX = ev.clientX;      //顶点原本的x值
                let h = box.clientHeight;   //box的高度
                let w = box.clientWidth;    //box的宽度
                let t = box.offsetTop;      //box距离顶部的top值
                let l = box.offsetLeft;     //box距离左边的left值
                window.onmousemove = function (e) {
                    let offsetY = e.clientY - oldY;         //鼠标移动y轴偏移量
                    let offsetX = e.clientX - oldX;         //鼠标移动x轴偏移量
                    //if条件是判断鼠标点击的节点目标对象
                    if (i == 0) {
                        box.style.height = `${h - offsetY + bd}px`;     //box高度变化
                        box.style.top = `${t + offsetY - bd}px`;        //box的top值变化(定位)
                        box.style.width = `${w - offsetX + bd}px`;      //box的宽度变化
                        box.style.left = `${l + offsetX - bd}px`;       //box的left值变化(定位)
                    } else if (i == 1) {
                        box.style.height = `${h - offsetY + bd}px`;
                        box.style.top = `${t + offsetY - bd}px`;
                        box.style.width = `${w + offsetX + bd}px`;
                    } else if (i == 2) {
                        box.style.height = `${h + offsetY + bd}px`;
                        box.style.width = `${w + offsetX + bd}px`;
                    } else if (i == 3) {
                        box.style.height = `${h + offsetY + bd}px`;
                        box.style.width = `${w - offsetX + bd}px`;
                        box.style.left = `${l + offsetX - bd}px`;
                    } else if (i == 4) {
                        box.style.height = `${h - offsetY + bd}px`;
                        box.style.top = `${t + offsetY - bd}px`;
                    } else if (i == 5) {
                        box.style.width = `${w + offsetX + bd}px`;
                    } else if (i == 6) {
                        box.style.height = `${h + offsetY + bd}px`;
                    } else {
                        box.style.width = `${w - offsetX + bd}px`;
                        box.style.left = `${l + offsetX - bd}px`;
                    }
                    //鼠标抬起后清除鼠标移动事件
                    window.onmouseup = function () {
                        window.onmousemove = false;
                    }
                }
            }
        }
    </script>
</body>
</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值