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 相关文章推荐
常用参考资料(手册)下载或者链接
Jul 22 Javascript
jQuery Ajax之$.get()方法和$.post()方法
Oct 12 Javascript
js使下拉列表框可编辑不止是选择
Dec 12 Javascript
使用CamanJS在Web页面上处理图像的技巧
Aug 18 Javascript
angularjs表格ng-table使用备忘录
Mar 09 Javascript
jquery插件格式实例分析
Jun 16 Javascript
Bootstrap Paginator+PageHelper实现分页效果
Dec 29 Javascript
JS尾递归的实现方法及代码优化技巧
Jan 19 Javascript
解决echarts中横坐标值显示不全(自动隐藏)问题
Jul 20 Javascript
JavaScript实现轮播图效果
Oct 30 Javascript
基于Vant UI框架实现时间段选择器
Dec 24 Javascript
vue3如何优雅的实现移动端登录注册模块
Mar 29 Vue.js
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
绿山咖啡和蓝山咖啡
2021/03/04 新手入门
php中hashtable实现示例分享
2014/02/13 PHP
thinkPHP实现瀑布流的方法
2014/11/29 PHP
php查询内存信息操作示例
2019/05/09 PHP
thinkphp 5框架实现登陆,登出及session登陆状态检测功能示例
2019/10/10 PHP
mailto的使用技巧分享
2012/12/21 Javascript
关闭ie窗口清除Session的解决方法
2014/01/10 Javascript
Jquery获取和修改img的src值的方法
2014/02/17 Javascript
jQuery级联操作绑定事件实例
2014/09/02 Javascript
JavaScript实现点击自动选择TextArea文本的方法
2015/07/02 Javascript
微信小程序数据分析之自定义分析的实现
2018/08/17 Javascript
原生js实现Flappy Bird小游戏
2018/12/24 Javascript
JavaScript中concat复制数组方法浅析
2019/01/20 Javascript
在 Vue 应用中使用 Netlify 表单功能的方法详解
2019/06/03 Javascript
[52:03]DOTA2-DPC中国联赛 正赛 Ehome vs iG BO3 第三场 1月31日
2021/03/11 DOTA
github配置使用指南
2014/11/18 Python
python实现基于两张图片生成圆角图标效果的方法
2015/03/26 Python
python类中super()和__init__()的区别
2016/10/18 Python
Python构建网页爬虫原理分析
2017/12/19 Python
Scrapy基于selenium结合爬取淘宝的实例讲解
2018/06/13 Python
tensorflow 获取模型所有参数总和数量的方法
2018/06/14 Python
Django中的ajax请求
2018/10/19 Python
Python机器学习之scikit-learn库中KNN算法的封装与使用方法
2018/12/14 Python
Python中的正则表达式与JSON数据交换格式
2019/07/03 Python
python实现简单聊天室功能 可以私聊
2019/07/12 Python
Django工程的分层结构详解
2019/07/18 Python
python自定义时钟类、定时任务类
2021/02/22 Python
Python时间差中seconds和total_seconds的区别详解
2019/12/26 Python
基于matplotlib xticks用法详解
2020/04/16 Python
HTML5 表单验证失败的提示语问题
2017/07/13 HTML / CSS
斯洛伐克电子产品购物网站:DATART
2020/04/05 全球购物
浅谈react路由传参的几种方式
2021/03/23 Javascript
幼儿园门卫岗位职责
2014/02/14 职场文书
教师节主持词开场白
2015/05/29 职场文书
党支部考察意见范文
2015/06/02 职场文书
周一给客户的问候语
2015/11/10 职场文书