jquery实现拖拽调整Div大小


Posted in Javascript onJanuary 30, 2015

今天写了一天这个jquery插件:

可以实现对div进行拖拽来调整大小的功能。

(function ($) {

    $.fn.dragDivResize = function () {

        var deltaX, deltaY, _startX, _startY;

        var resizeW, resizeH;

        var size = 20;

        var minSize = 10;

        var scroll = getScrollOffsets();

        var _this = this;

        for (var i = 0; i < _this.length; i++) {

            var target = this[i];

            $(target).on("mouseover mousemove", overHandler);

        }

        function outHandler() {

            for (var i = 0; i < _this.length; i++) {

                target.style.outline = "none";

            }

            document.body.style.cursor = "default";

        }

        function overHandler(event) {

            target = event.target || event.srcElement;

            var startX = event.clientX + scroll.x;

            var startY = event.clientY + scroll.y;

            var w = $(target).width();

            var h = $(target).height();

            _startX = parseInt(startX);

            _startY = parseInt(startY);

            if ((0 < target.offsetLeft + w - _startX && target.offsetLeft + w - _startX < size) || (0 < target.offsetTop + h - _startY && target.offsetTop + h - _startY < size)) {

                target.style.outline = "2px dashed #333";

                if ((0 > target.offsetLeft + w - _startX || target.offsetLeft + w - _startX > size) && 0 < target.offsetTop + h - _startY && target.offsetTop + h - _startY < size) {

                    resizeW = false;

                    resizeH = true;

                    document.body.style.cursor = "s-resize";

                }

                if (0 < target.offsetLeft + w - _startX && target.offsetLeft + w - _startX < size && (0 > target.offsetTop + h - _startY || target.offsetTop + h - _startY > size)) {

                    resizeW = true;

                    resizeH = false;

                    document.body.style.cursor = "w-resize";

                }

                if (0 < target.offsetLeft + w - _startX && target.offsetLeft + w - _startX < size && 0 < target.offsetTop + h - _startY && target.offsetTop + h - _startY < size) {

                    resizeW = true;

                    resizeH = true;

                    document.body.style.cursor = "se-resize";

                }

                $(target).on('mousedown', downHandler);

            } else {

                resizeW = false;

                resizeH = false;

                $(target).off('mousedown', downHandler);

            }

        }

        function downHandler(event) {

            target = event.target || event.srcElement;

            var startX = event.clientX + scroll.x;

            var startY = event.clientY + scroll.y;

            _startX = parseInt(startX);

            _startY = parseInt(startY);

            if (document.addEventListener) {

                document.addEventListener("mousemove", moveHandler, true);

                document.addEventListener("mouseup", upHandler, true);

            } else if (document.attachEvent) {

                target.setCapture();

                target.attachEvent("onlosecapeture", upHandler);

                target.attachEvent("onmouseup", upHandler);

                target.attachEvent("onmousemove", moveHandler);

            }

            if (event.stopPropagation) {

                event.stopPropagation();

            } else {

                event.cancelBubble = true;

            }

            if (event.preventDefault) {

                event.preventDefault();

            } else {

                event.returnValue = false;

            }

        }

        function moveHandler(e) {

            if (!e) e = window.event;

            var w, h;

            var startX = parseInt(e.clientX + scroll.x);

            var startY = parseInt(e.clientY + scroll.y);

            target = target || e.target || e.srcElement;

            if (target == document.body) {

                return;

            }

            if (resizeW) {

                deltaX = startX - _startX;

                w = $(target).width() + deltaX < minSize ? minSize : $(target).width() + deltaX;

                target.style.width = w + "px";

                _startX = startX;

            }

            if (resizeH) {

                deltaY = startY - _startY;

                h = $(target).height() + deltaY < minSize ? minSize : $(target).height() + deltaY;

                target.style.height = h + "px";

                _startY = startY;

            }

            if (e.stopPropagation) {

                e.stopPropagation();

            } else {

                e.cancelBubble = true;

            }

        }

        function upHandler(e) {

            if (!e) {

                e = window.event;

            }

            resizeW = false;

            resizeH = false;

            target = e.target || e.srcElement;

            $(target).on("mouseout", outHandler);

            if (document.removeEventListener) {

                document.removeEventListener("mousemove", moveHandler, true);

                document.removeEventListener("mouseup", upHandler, true);

            } else if (document.detachEvent) {

                target.detachEvent("onlosecapeture", upHandler);

                target.detachEvent("onmouseup", upHandler);

                target.detachEvent("onmousemove", moveHandler);

                target.releaseCapture();

            }

            if (e.stopPropagation) {

                e.stopPropagation();

            } else {

                e.cancelBubble = true;

            }

        }

        function getScrollOffsets(w) {

            w = w || window;

            if (w.pageXOffset != null) {

                return { x: w.pageXOffset, y: w.pageYOffset };

            }

            var d = w.document;

            if (document.compatMode == "CSS1Compat") {

                return { x: d.documentElement.scrollLeft, y: d.documentElement.scrollTop };

            }

            return { x: d.body.scrollLeft, y: d.body.scrollTop };

        }

    }

}(jQuery));

jQuery("div").dragDivResize();

 记录一下今天的劳动成果,可能会有很多不成熟的地方,欢迎大家来指正,谢谢!

以上就是本文的全部内容了,希望大家能够喜欢。

Javascript 相关文章推荐
juqery 学习之五 文档处理 包裹、替换、删除、复制
Feb 11 Javascript
基于jQuery的为attr添加id title等效果的实现代码
Apr 20 Javascript
JS实现侧悬浮浮动实例代码
Nov 29 Javascript
jquery防止重复执行动画避免页面混乱
Apr 22 Javascript
原创jQuery弹出层插件分享
Apr 02 Javascript
JavaScript 实现完美兼容多浏览器的复制功能代码
Apr 28 Javascript
node.js回调函数之阻塞调用与非阻塞调用
Nov 13 Javascript
JS密码生成与强度检测完整实例(附demo源码下载)
Apr 06 Javascript
JS实现的随机排序功能算法示例
Jun 09 Javascript
详解小程序中h5页面onShow实现及跨页面通信方案
May 30 Javascript
Vue获取页面元素的相对位置的方法示例
Feb 05 Javascript
vue-cli3中配置alias和打包加hash值操作
Sep 04 Javascript
jQuery中$.click()无效问题分析
Jan 29 #Javascript
Eclipse配置Javascript开发环境图文教程
Jan 29 #Javascript
js操作滚动条事件实例
Jan 29 #Javascript
jQuery调取jSon数据并展示的方法
Jan 29 #Javascript
jquery通过load获取文件的内容并跳到锚点的方法
Jan 29 #Javascript
浅谈Javascript 数组与字典
Jan 29 #Javascript
javascript 数组操作详解
Jan 29 #Javascript
You might like
PHP 冒泡排序 二分查找 顺序查找 二维数组排序算法函数的详解
2013/06/25 PHP
PHP实现加密文本文件并限制特定页面的存取的效果
2016/10/21 PHP
详解PHP中的外观模式facade pattern
2018/02/05 PHP
PHP快速导出百万级数据到CSV或者EXCEL文件
2020/11/27 PHP
DOM 脚本编程中的兄弟节点
2009/10/31 Javascript
javascript 动态设置已知select的option的value值的代码
2009/12/16 Javascript
跟着JQuery API学Jquery 之三 筛选
2010/04/09 Javascript
jQuery帮助之筛选查找 children([expr])
2011/01/31 Javascript
js获取url参数值的两种方式
2013/09/10 Javascript
JS控制日期显示的小例子
2013/11/23 Javascript
jQuery.deferred对象使用详解
2016/03/18 Javascript
javascript显示倒计时控制按钮的简单实现
2016/06/07 Javascript
最全的Javascript编码规范(推荐)
2016/06/22 Javascript
使用vue.js制作分页组件
2016/06/27 Javascript
详解JS中的柯里化(currying)
2017/08/17 Javascript
JS内置对象和Math对象知识点详解
2020/04/03 Javascript
js在HTML的三种引用方式详解
2020/08/29 Javascript
小程序自定义弹框效果
2020/11/16 Javascript
[06:44]2018DOTA2亚洲邀请赛4.5 SOLO赛 MidOne vs Sumail
2018/04/06 DOTA
[46:14]VGJ.T vs Liquid 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
urllib2自定义opener详解
2014/02/07 Python
用Python编写分析Python程序性能的工具的教程
2015/04/01 Python
Python发送以整个文件夹的内容为附件的邮件的教程
2015/05/06 Python
python检查序列seq是否含有aset中项的方法
2015/06/30 Python
利用Python yagmail三行代码实现发送邮件
2018/05/11 Python
Python函数和模块的使用总结
2019/05/20 Python
CSS3 clip-path 用法介绍详解
2018/03/01 HTML / CSS
介绍下static、final、abstract区别
2015/01/30 面试题
汽车检测与维修应届毕业生求职信
2013/10/19 职场文书
计算机专业推荐信范文
2013/11/20 职场文书
行政前台岗位职责
2013/12/04 职场文书
大学生实习感言
2014/01/16 职场文书
教育技术职业规划范文
2014/03/04 职场文书
有子女的离婚协议书怎么写(范本)
2014/09/29 职场文书
python读取mnist数据集方法案例详解
2021/09/04 Python
工厂无线对讲系统解决方案
2022/02/18 无线电