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 相关文章推荐
javascript+iframe 实现无刷新载入整页的代码
Mar 17 Javascript
jQuery 美元符冲突的解决方法
Mar 28 Javascript
多种方法判断Javascript对象是否存在
Sep 22 Javascript
JavaScript的常见兼容问题及相关解决方法(chrome/IE/firefox)
Dec 31 Javascript
text-align:justify实现文本两端对齐 兼容IE
Aug 19 Javascript
jquery插件pagination实现无刷新ajax分页
Sep 30 Javascript
js省市联动效果完整实例代码
Dec 09 Javascript
jquery插件uploadify多图上传功能实现代码
Aug 12 Javascript
网站发布后Bootstrap框架引用woff字体无法正常显示的解决方法
Nov 24 Javascript
压缩Vue.js打包后的体积方法总结(Vue.js打包后体积过大问题)
Feb 03 Javascript
JSON 入门教程基础篇 json入门学习笔记
Sep 22 Javascript
JavaScript实现队列结构过程
Dec 06 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实现屏蔽掉黑帽SEO的搜索关键字
2015/04/15 PHP
PHP模板引擎Smarty内建函数foreach,foreachelse用法分析
2016/04/11 PHP
解决PHP程序运行时:Fatal error: Maximum execution time of 30 seconds exceeded in的错误提示
2016/11/25 PHP
Laravel5.* 打印出执行的sql语句的方法
2017/07/24 PHP
PHP使用Redis长连接的方法详解
2018/02/12 PHP
PHP面向对象程序设计重载(overloading)操作详解
2019/06/13 PHP
经典的带阴影的可拖动的浮动层
2006/06/26 Javascript
用Javascript实现Sleep暂停功能代码
2010/09/03 Javascript
JavaScript中this的使用详解
2013/11/08 Javascript
jquery实现侧边弹出的垂直导航
2014/12/09 Javascript
使用Sticker.js实现贴纸效果
2015/01/28 Javascript
JavaScript中的依赖注入详解
2015/03/18 Javascript
详解JavaScript中的表单验证
2015/06/16 Javascript
jQuery插件imgPreviewQs实现上传图片预览
2016/01/15 Javascript
学习Javascript面向对象编程之封装
2016/02/23 Javascript
Select下拉框模糊查询功能实现代码
2016/07/22 Javascript
JavaScript代码执行的先后顺序问题
2017/10/29 Javascript
Python使用PIL库实现验证码图片的方法
2016/03/11 Python
基于python yield机制的异步操作同步化编程模型
2016/03/18 Python
Python 中pandas.read_excel详细介绍
2017/06/23 Python
分析python动态规划的递归、非递归实现
2018/03/04 Python
解决Pycharm中import时无法识别自己写的程序方法
2018/05/18 Python
基于Python对数据shape的常见操作详解
2018/12/25 Python
对python 树状嵌套结构的实现思路详解
2019/08/09 Python
python opencv圆、椭圆与任意多边形的绘制实例详解
2020/02/06 Python
Python利用逻辑回归分类实现模板
2020/02/15 Python
查看已安装tensorflow版本的方法示例
2020/04/19 Python
用纯css3和html制作泡沫对话框实现代码
2013/03/21 HTML / CSS
哥伦比亚最大的网上商店:Linio哥伦比亚
2016/09/25 全球购物
什么是GWT的Entry Point
2013/08/16 面试题
仓库管理专业个人的自我评价
2013/12/30 职场文书
工作睡觉检讨书
2014/02/25 职场文书
2014年秋季开学典礼主持词
2014/08/02 职场文书
学校青年志愿者活动总结
2015/05/06 职场文书
机关工会工作总结2015
2015/05/26 职场文书
六年级上册《闻官军收河南河北》的教学设计
2019/11/15 职场文书