js实现元素拖拽四个角以及四个边改变大小以及拖拽中间主体可移动

下方有代码哦~

效果图:

js实现元素拖拽四个角以及四个边改变大小以及拖拽中间主体可移动

功能介绍图:

js实现元素拖拽四个角以及四个边改变大小以及拖拽中间主体可移动

代码实现:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        * {
            margin: 0;
            padding: 0
        }
        .box {
            width: 300px;
            height: 300px;
            position: absolute;
            top: 100px;
            left: 100px;
        }
        /*四边*/
        .box .t,
        .box .b,
        .box .l,
        .box .r {
            position: absolute;
            z-index: 1;
            background: #666;
        }
        .box .l,
        .box .r {
            width: 5px;
            height: 100%;
            cursor: col-resize;
        }
        .box .t,
        .box .b {
            width: 100%;
            height: 5px;
            cursor: row-resize;
        }
        .box .t {
            top: 0;
        }
        .box .b {
            bottom: 0;
        }
        .box .l {
            left: 0;
        }
        .box .r {
            right: 0;
        }
        /*四角*/
        .box .tl,
        .box .bl,
        .box .br,
        .box .tr {
            width: 20px;
            height: 20px;
            position: absolute;
            background: #fff;
            border:1px solid #666;
            z-index: 2;
            cursor: nwse-resize
        }
        .box .tl,
        .box .bl {
            left: -5px;
        }
        .box .tr,
        .box .br {
            right: -5px;
        }
        .box .br,
        .box .bl {
            bottom: -5px;
        }
        .box .tl,
        .box .tr {
            top: -5px;
        }
        .box .tr,
        .box .bl {
            cursor: nesw-resize;
        }
        /*内核*/
        .inner {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <!-- 将边线的缩放也加了 -->
    <div class="box" id="odiv">
        <span class="r"></span>
        <span class="l"></span>
        <span class="t"></span>
        <span class="b"></span>
        <span class="br"></span>
        <span class="bl"></span>
        <span class="tr"></span>
        <span class="tl"></span>
        <div class="inner"></div>
    </div>
    <script>
        window.onload = function () {
            var oDiv = document.getElementsByTagName('div')[0];
            oDiv.style.width = '500px';
            var aSpan = oDiv.getElementsByTagName('span');
            for (var i = 0; i < aSpan.length; i++) {
                dragFn(aSpan[i]);
            }
            function dragFn(obj) {
                obj.onmousedown = function (ev) {
                    var oEv = ev || event;
                    oEv.stopPropagation();
                    var oldWidth = oDiv.offsetWidth;
                    var oldHeight = oDiv.offsetHeight;
                    var oldX = oEv.clientX;
                    var oldY = oEv.clientY;
                    var oldLeft = oDiv.offsetLeft;
                    var oldTop = oDiv.offsetTop;

                    document.onmousemove = function (ev) {
                        var oEv = ev || event;
                        let disY = (oldTop + (oEv.clientY - oldY)),
                            disX = (oldLeft + (oEv.clientX - oldLeft));
                        if(disX>oldLeft+oldWidth){
                            disX=oldLeft+oldWidth
                        }
                        if(disY>oldTop+oldHeight){
                            disY=oldTop+oldHeight
                        }
                        if (obj.className == 'tl') {
                            oDiv.style.width = oldWidth - (oEv.clientX - oldX) + 'px';
                            oDiv.style.height = oldHeight - (oEv.clientY - oldY) + 'px';
                            oDiv.style.left = disX + 'px';
                            oDiv.style.top = disY + 'px';
                        }
                        else if (obj.className == 'bl') {
                            oDiv.style.width = oldWidth - (oEv.clientX - oldX) + 'px';
                            oDiv.style.height = oldHeight + (oEv.clientY - oldY) + 'px';
                            oDiv.style.left = disX + 'px';
                            oDiv.style.bottom = oldTop + (oEv.clientY + oldY) + 'px';
                        }
                        else if (obj.className == 'tr') {
                            oDiv.style.width = oldWidth + (oEv.clientX - oldX) + 'px';
                            oDiv.style.height = oldHeight - (oEv.clientY - oldY) + 'px';
                            oDiv.style.right = oldLeft - (oEv.clientX - oldX) + 'px';
                            oDiv.style.top = disY + 'px';
                        }
                        else if (obj.className == 'br') {
                            oDiv.style.width = oldWidth + (oEv.clientX - oldX) + 'px';
                            oDiv.style.height = oldHeight + (oEv.clientY - oldY) + 'px';
                            oDiv.style.right = oldLeft - (oEv.clientX - oldX) + 'px';
                            oDiv.style.bottom = oldTop + (oEv.clientY + oldY) + 'px';
                        }
                        else if (obj.className == 't') {
                            oDiv.style.height = oldHeight - (oEv.clientY - oldY) + 'px';
                            oDiv.style.top = disY + 'px';
                        }
                        else if (obj.className == 'b') {
                            oDiv.style.height = oldHeight + (oEv.clientY - oldY) + 'px';
                            oDiv.style.bottom = disY + 'px';
                        }
                        else if (obj.className == 'l') {
                            oDiv.style.height = oldHeight + 'px';
                            oDiv.style.width = oldWidth - (oEv.clientX - oldX) + 'px';
                            oDiv.style.left = disX + 'px';
                        }
                        else if (obj.className == 'r') {
                            oDiv.style.height = oldHeight + 'px';
                            oDiv.style.width = oldWidth + (oEv.clientX - oldX) + 'px';
                            oDiv.style.right = disX + 'px';
                        }
                    };

                    document.onmouseup = function () {
                        document.onmousemove = null;
                    };
                    return false;
                };
            }
            document.getElementById("odiv").onmousedown = function (ev) {
                var oevent = ev || event;
                oevent.preventDefault();

                var distanceX = oevent.clientX - oDiv.offsetLeft;
                var distanceY = oevent.clientY - oDiv.offsetTop;

                document.onmousemove = function (ev) {
                    var oevent = ev || event;
                    oDiv.style.left = oevent.clientX - distanceX + 'px';
                    oDiv.style.top = oevent.clientY - distanceY + 'px';
                };
                document.onmouseup = function () {
                    document.onmousemove = null;
                    document.onmouseup = null;
                };
            }
        };
    </script>
</body>
</html>
如果此篇文章帮助到你了请点赞关注后再走哦~这样才能增加文章的访问量帮助到更多的朋友哦 ~
上一篇:匀速透明封装(透明度)


下一篇:简易日历(innerHTML)