js 实现div拖拽拉伸完整示例

 更新时间:2022年10月13日 10:16:00   作者:名字起太长会有傻子跟着念  
这篇文章主要为大家介绍了js 实现div拖拽拉伸完整示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前言

今天和大家分享一下。如何用js实现div拖拽拉伸等功能。功能比较简单,我就不赘述了。直接上代码。

HTML

<div class="resize" data-key="drag">
    <img src="../images/liya.jpg" alt="">
    <div class="line line-n" data-key="n"></div>
    <div class="line line-e" data-key="e"></div>
    <div class="line line-s" data-key="s"></div>
    <div class="line line-w" data-key="w"></div>
    <div class="point point-n" data-key="n"></div>
    <div class="point point-e" data-key="e"></div>
    <div class="point point-s" data-key="s"></div>
    <div class="point point-w" data-key="w"></div>
    <div class="point point-ne" data-key="ne"></div>
    <div class="point point-se" data-key="se"></div>
    <div class="point point-sw" data-key="sw"></div>
    <div class="point point-nw" data-key="nw"></div>
</div>

CSS

* {
    margin: 0;
    padding: 0;
}
.container {
    height: 100vh;
    background: #000;
    overflow: hidden;
    user-select: none;
}
.resize {
    position: relative;
    outline: 1px solid #1890ff;
    touch-action: none;
}
img {
    display: block;
    width: 100%;
    height: 100%;
    pointer-events: none;
}
.line {
    position: absolute;
}
.line-n {
    left: 0;
    top: -3px;
    width: 100%;
    height: 6px;
    cursor: n-resize;
}
.line-e {
    top: 0;
    right: -3px;
    width: 6px;
    height: 100%;
    cursor: e-resize;
}
.line-s {
    left: 0;
    bottom: -3px;
    width: 100%;
    height: 6px;
    cursor: s-resize;
}
.line-w {
    top: 0;
    left: -3px;
    width: 6px;
    height: 100%;
    cursor: w-resize;
}
.point {
    position: absolute;
    width: 6px;
    height: 6px;
    background: #1890ff;
}
.point-n {
    top: -3px;
    left: calc(50% - 3px);
    cursor: n-resize;
}
.point-e {
    right: -3px;
    top: calc(50% - 3px);
    cursor: e-resize;
}
.point-s {
    bottom: -3px;
    left: calc(50% - 3px);
    cursor: s-resize;
}
.point-w {
    left: -3px;
    top: calc(50% - 3px);
    cursor: w-resize;
}
.point-ne {
    top: -3px;
    right: -3px;
    cursor: ne-resize;
}
.point-se {
    bottom: -3px;
    right: -3px;
    cursor: se-resize;
}
.point-sw {
    left: -3px;
    bottom: -3px;
    cursor: sw-resize;
}
.point-nw {
    left: -3px;
    top: -3px;
    cursor: nw-resize;
}

JS

// 获取dom
const box = document.querySelector('.resize');
// 声明全局变量
let width = 200, height = 160, minWidth = 100, minHeight = 80, isPointerdown = false,
    x = (window.innerWidth - width) * 0.5, y = (window.innerHeight - height) * 0.5,
    diff = { x: 0, y: 0 }, lastPointermove = { x: 0, y: 0 }, key = '', rect = null;
box.style.width = width + 'px';
box.style.height = height + 'px';
box.style.transform = 'translate3d(' + x + 'px, ' + y + 'px, 0px)';
const action = {
    drag: function () {
        x += diff.x;
        y += diff.y;
    },
    n: function (e) {
        if (rect.bottom - e.clientY > minHeight) {
            height = rect.bottom - e.clientY;
            y = e.clientY;
        }
    },
    e: function (e) {
        if (e.clientX - rect.left > minWidth) {
            width = e.clientX - rect.left;
        }
    },
    s: function (e) {
        if (e.clientY - rect.top > minHeight) {
            height = e.clientY - rect.top;
        }
    },
    w: function (e) {
        if (rect.right - e.clientX > minWidth) {
            width = rect.right - e.clientX;
            x = e.clientX;
        }
    },
    ne: function (e) {
        this.n(e);
        this.e(e);
    },
    se: function (e) {
        this.s(e);
        this.e(e);
    },
    sw: function (e) {
        this.s(e);
        this.w(e);
    },
    nw: function (e) {
        this.n(e);
        this.w(e);
    }
}
// 绑定事件
box.addEventListener('pointerdown', function (e) {
    isPointerdown = true;
    e.target.setPointerCapture(e.pointerId);
    lastPointermove = { x: e.clientX, y: e.clientY };
    key = e.target.dataset.key;
    rect = box.getBoundingClientRect();
});
box.addEventListener('pointermove', function (e) {
    if (isPointerdown) {
        const current = { x: e.clientX, y: e.clientY };
        diff.x = current.x - lastPointermove.x;
        diff.y = current.y - lastPointermove.y;
        lastPointermove = { x: current.x, y: current.y };
        action[key](e);
        box.style.width = width + 'px';
        box.style.height = height + 'px';
        box.style.transform = 'translate3d(' + x + 'px, ' + y + 'px, 0)';
        e.preventDefault();
    }
});
box.addEventListener('pointerup', function (e) {
    if (isPointerdown) {
        isPointerdown = false;
    }
});
box.addEventListener('pointercancel', function (e) {
    if (isPointerdown) {
        isPointerdown = false;
    }
});

Demo: jsdemo.codeman.top/html/divRes…

以上就是js 实现div拖拽拉伸完整示例的详细内容,更多关于js 实现div拖拽拉伸的资料请关注脚本之家其它相关文章!

相关文章

  • pnpm install:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies

    pnpm install:ERR_PNPM_PEER_DEP_ISSUES Unmet p

    这篇文章主要为大家介绍了pnpm install:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies报错解决
    2023-06-06
  • es6 filter() 数组过滤方法总结

    es6 filter() 数组过滤方法总结

    这篇文章主要介绍了es6 filter() 数组过滤方法总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • 为调试JavaScript添加输出窗口的代码

    为调试JavaScript添加输出窗口的代码

    调试JavaScript是一件很麻烦的事,尽管有很多很好用的调试工具,但有时候想要跟踪值的变化,但即不想中断脚本执行,也不想用alert显示值信息,这种情况下,一般的做法是在页面上添加一个DIV或者其它元素,然后再往里面添加调试信息。
    2010-02-02
  • js实现圆盘记速表

    js实现圆盘记速表

    本文分享了一个利用AmCharts制作的汽车速度计速表,通过设置不同的速度(数字)来动态改变计速表的指针。使用也非常简单,下面给出方法。
    2015-08-08
  • JavaScript插入排序算法原理与实现方法示例

    JavaScript插入排序算法原理与实现方法示例

    这篇文章主要介绍了JavaScript插入排序算法原理与实现方法,简单分析了插入排序的概念、原理并结合实例形式分析了JavaScript插入排序算法的具体实现技巧与注意事项,需要的朋友可以参考下
    2018-08-08
  • JavaScript 类的封装操作示例详解

    JavaScript 类的封装操作示例详解

    这篇文章主要介绍了JavaScript 类的封装操作,结合实例形式分析了JavaScript 类的封装基本原理、操作技巧与相关注意事项,需要的朋友可以参考下
    2020-05-05
  • js加减乘除精确运算方法实例代码

    js加减乘除精确运算方法实例代码

    这篇文章主要给大家介绍了关于js加减乘除精确运算方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Javascript 实现 Excel 导入生成图表功能

    Javascript 实现 Excel 导入生成图表功能

    这篇文章主要介绍了Javascript 实现 Excel 导入生成图表功能,本文通过实例代码讲解给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-10-10
  • JS获取指定时间的时间戳的方法汇总(最新整理收藏版)

    JS获取指定时间的时间戳的方法汇总(最新整理收藏版)

    在JavaScript中,可以使用Date.parse()或new Date()来获取指定时间的时间戳,本文给大家分享JS获取指定时间的时间戳的方法,感兴趣的朋友一起看看吧
    2024-01-01
  • js中的document.querySelector()方法举例详解

    js中的document.querySelector()方法举例详解

    这篇文章主要给大家介绍了关于js中document.querySelector()方法的相关资料,document.querySelector是JavaScript中的一个内置方法,用于通过CSS选择器选择文档中的第一个匹配元素,需要的朋友可以参考下
    2024-01-01

最新评论