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 相关文章推荐
js每次Title显示不同的名言
Sep 25 Javascript
javascript FormatNumber函数实现方法
Dec 30 Javascript
jQuery防止click双击多次提交及传递动态函数或多参数
Apr 02 Javascript
jQuery获取上传文件的名称的正则表达式
May 21 Javascript
Jquery $when done then的用法详解
May 20 Javascript
JavaScript DOM 对象深入了解
Jul 20 Javascript
Node.js如何自动审核团队的代码
Jul 20 Javascript
使用JavaScript为一张图片设置备选路径的方法
Jan 04 Javascript
vue-cli创建的项目,配置多页面的实现方法
Mar 15 Javascript
webpack 模块热替换原理
Apr 09 Javascript
不得不知的ES6小技巧
Jul 28 Javascript
HTML+JS实现“代码雨”效果源码(黑客帝国文字下落效果)
Mar 17 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/05/10 PHP
遭遇php的in_array低性能问题
2013/09/17 PHP
php生成随机颜色的方法
2014/11/13 PHP
PHP合并静态文件详解
2014/11/14 PHP
ThinkPHP框架表单验证操作方法
2017/07/19 PHP
php删除一个路径下的所有文件夹和文件的方法
2018/02/07 PHP
javascript上传图片前预览图片兼容大多数浏览器
2013/10/25 Javascript
sliderToggle在写jquery的计时器setTimeouter中不生效
2014/05/26 Javascript
javascript拖拽应用实例
2016/03/25 Javascript
javascript 中的事件委托详解
2016/10/25 Javascript
BACKBONE.JS 简单入门范例
2017/10/17 Javascript
jquery学习笔记之无new构建详解
2017/12/07 jQuery
Vue cli+mui 区域滚动的实例代码
2018/01/25 Javascript
javascript读取本地文件和目录方法详解
2020/08/06 Javascript
Javascript中的奇葩知识,你知道吗?
2021/01/25 Javascript
python应用程序在windows下不出现cmd窗口的办法
2014/05/29 Python
使用Python的Flask框架实现视频的流媒体传输
2015/03/31 Python
Python中decorator使用实例
2015/04/14 Python
python3+PyQt5实现支持多线程的页面索引器应用程序
2018/04/20 Python
python验证码识别教程之利用滴水算法分割图片
2018/06/05 Python
Python 实现某个功能每隔一段时间被执行一次的功能方法
2018/10/14 Python
Python 安装第三方库 pip install 安装慢安装不上的解决办法
2019/06/18 Python
Django-xadmin后台导入json数据及后台显示信息图标和主题更改方式
2020/03/11 Python
HTML5中的Article和Section元素认识及使用
2013/03/22 HTML / CSS
HTML5不支持frameset的两种解决方法
2016/11/14 HTML / CSS
小程序canvas中文字设置居中锚点
2019/04/16 HTML / CSS
波兰在线儿童和婴儿用品零售商:pinkorblue
2019/06/29 全球购物
值传递还是引用传递
2015/02/08 面试题
学校安全生产承诺书
2014/05/23 职场文书
居委会个人对照检查材料思想汇报
2014/09/29 职场文书
2014年乡镇党建工作总结
2014/11/11 职场文书
公司财务经理岗位职责
2015/04/08 职场文书
redis三种高可用方式部署的实现
2021/05/11 Redis
只用50行Python代码爬取网络美女高清图片
2021/06/02 Python
SpringBoot中获取profile的方法详解
2022/04/08 Java/Android
pandas时间序列之pd.to_datetime()的实现
2022/06/16 Python