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拖拽拉伸的资料请关注脚本之家其它相关文章!

相关文章

  • js实现幻灯片效果(基于jquery插件)

    js实现幻灯片效果(基于jquery插件)

    说起幻灯片效果,想必大家都不陌生吧,接下来为大家介绍下使用jquery插件jquery.smallslider.js实现幻灯片效果示例代码,喜欢的朋友可以学习下
    2013-11-11
  • JS简单实现获取元素的封装操作示例

    JS简单实现获取元素的封装操作示例

    这篇文章主要介绍了JS简单实现获取元素的封装操作,结合实例形式分析了JS针对页面ID、class、TAG元素获取的函数与对象封装操作实现方法,需要的朋友可以参考下
    2017-04-04
  • Bootstrap安装环境配置教程分享

    Bootstrap安装环境配置教程分享

    这篇文章主要为大家分享了Bootstrap安装环境配置教程,安装步骤非常简单,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • 基于bootstrap按钮式下拉菜单组件的搜索建议插件

    基于bootstrap按钮式下拉菜单组件的搜索建议插件

    这篇文章主要为大家详细介绍了基于bootstrap按钮式下拉菜单组件的搜索建议插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • 微信小程序云开发之模拟后台增删改查

    微信小程序云开发之模拟后台增删改查

    这篇文章主要为大家详细介绍了微信小程序云开发之模拟后台增删改查,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • 利用jsonp与代理服务器方案解决跨域问题

    利用jsonp与代理服务器方案解决跨域问题

    这篇文章主要给大家介绍了关于利用jsonp与代理服务器方案解决跨域问题的相关资料,文中通过示例代码给大家介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-09-09
  • Kindeditor单独调用多图上传实例

    Kindeditor单独调用多图上传实例

    下面小编就为大家带来一篇Kindeditor单独调用多图上传实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • js实现的捐赠管理完整实例

    js实现的捐赠管理完整实例

    这篇文章主要介绍了js实现的捐赠管理完整实例,包括了html页面、js脚本及css样式的完整实现代码,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • 解决layer.open后laydate失效的问题

    解决layer.open后laydate失效的问题

    今天小编就为大家分享一篇解决layer.open后laydate失效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • 使用phantomjs进行网页抓取的实现代码

    使用phantomjs进行网页抓取的实现代码

    这篇文章主要介绍了使用phantomjs进行网页抓取的实现代码,需要的朋友可以参考下
    2014-09-09

最新评论