利用原生JS实现“图片可拖拽,并且移动四个角和四条边能够自由变换图片大小功能”代码

这也是我在网上精挑细选搜出来的资料,本人亲测有效,大家放心使用。

代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        * {
            margin: 0;
            padding: 0
        }
 
        .box {
            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: 10px;
            height: 100%;
            cursor: col-resize;
        }
 
        .box .t,
        .box .b {
            width: 100%;
            height: 10px;
            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: #CCC;
            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;
        }
 
        /*图片*/
        img {
            width: 100%;
            height: 100%;
        }
    </style>
    <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;
 
                    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;
 
                        if (obj.className == 'tl') {
                            oDiv.style.width = oldWidth - (oEv.clientX - oldX) + 'px';
                            oDiv.style.height=oldHeight-(oEv.clientY-oldY)+'px';
                            oDiv.style.left = oldLeft + (oEv.clientX - oldX) + 'px';
                            oDiv.style.top = oldTop + (oEv.clientY - oldY) + '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 = oldLeft + (oEv.clientX - oldX) + '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 = oldTop + (oEv.clientY - oldY) + '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 = oldTop + (oEv.clientY - oldY) + 'px';
                        }
                        else if (obj.className == 'b') {
                            oDiv.style.height = oldHeight + (oEv.clientY - oldY)+'px';
                            oDiv.style.bottom = oldTop - (oEv.clientY + oldY) + 'px';
                        }
                        else if (obj.className == 'l') {
                            oDiv.style.height = oldHeight + 'px';
                            oDiv.style.width = oldWidth - (oEv.clientX - oldX) + 'px';
                            oDiv.style.left = oldLeft + (oEv.clientX - oldX) + 'px';
                        }
                        else if (obj.className == 'r') {
                            oDiv.style.height = oldHeight + 'px';
                            oDiv.style.width = oldWidth + (oEv.clientX - oldX) + 'px';
                            oDiv.style.right = oldLeft - (oEv.clientX - oldX) + 'px';
                        }
                    };
 
                    document.onmouseup = function () {
                        document.onmousemove = null;
                    };
                    return false;
                };
            }
 
        };
    </script>
</head>
 
<body>
<div class="box">
    <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>
    <img src="https://www.fujieace.com/wp-content/themes/fujie/img/logo.png?x86494">
</div>
</body>
</html>

js自动改变图片大小代码

    A+
发布日期:2017年11月05日  所属分类:javascript
最后修改时间:2017-12-15 19:18
评分: (当前没有评级)
PHP运行环境 Wamp Lamp Lnmp 安装、配置、搭建服务
PHP运行环境 Wamp Lamp Lnmp 安装、配置、搭建服务
  • ¥ 9.9元
  • 市场价:49.9元
永久免费SSL证书服务 HTTPS申请 安装 配置 支持通配符*
永久免费SSL证书服务 HTTPS申请 安装 配置 支持通配符*
  • ¥ 199.9元
  • 市场价:20000元
花牛苹果 甘肃天水 李宏恩家自种 1斤 包邮
花牛苹果 甘肃天水 李宏恩家自种 1斤 包邮
  • ¥ 6.8元
  • 市场价:8.8元
wordpress站群服务 泛解析二级域名站群 二级目录站群
wordpress站群服务 泛解析二级域名站群 二级目录站群
  • ¥ 1999.9元
  • 市场价:4800元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: