博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个简单的拖放控件
阅读量:5774 次
发布时间:2019-06-18

本文共 6996 字,大约阅读时间需要 23 分钟。

  hot3.png

Javascript文件

/** * @description 拖放控件 * @author {pangkaiguo} * @date {2013-06-26} */var Drag_isIE = (document.all) ? true : false;var Drag_$ = function(id) {    return "string" == typeof id ? document.getElementById(id) : id;};var Drag_Class = {    create : function() {        return function() {            this.initialize.apply(this, arguments);        }    }}var Drag_Extend = function(destination, source) {    for (var property in source) {        destination[property] = source[property];    }}var Drag_Bind = function(object, fun) {    return function() {        return fun.apply(object, arguments);    }}var Drag_BindAsEventListener = function(object, fun) {    return function(event) {        return fun.call(object, (event || window.event));    }}var Drag_CurrentStyle = function(element) {    return element.currentStyle || document.defaultView.getComputedStyle(element, null);}function addEventHandler(oTarget, sEventType, fnHandler) {    if (oTarget.addEventListener) {        oTarget.addEventListener(sEventType, fnHandler, false);    } else if (oTarget.attachEvent) {        oTarget.attachEvent("on" + sEventType, fnHandler);    } else {        oTarget["on" + sEventType] = fnHandler;    }};function removeEventHandler(oTarget, sEventType, fnHandler) {    if (oTarget.removeEventListener) {        oTarget.removeEventListener(sEventType, fnHandler, false);    } else if (oTarget.detachEvent) {        oTarget.detachEvent("on" + sEventType, fnHandler);    } else {        oTarget["on" + sEventType] = null;    }};//拖放程序var Drag = Drag_Class.create();Drag.prototype = {    //拖放对象    initialize : function(drag, options) {        this.Drag = Drag_$(drag);        //拖放对象        this._x = this._y = 0;        //记录鼠标相对拖放对象的位置        this._marginLeft = this._marginTop = 0;        //记录margin        //事件对象(用于绑定移除事件)        this._fM = Drag_BindAsEventListener(this, this.Move);        this._fS = Drag_Bind(this, this.Stop);        this.SetOptions(options);        this.Limit = !!this.options.Limit;        this.mxLeft = parseInt(this.options.mxLeft);        this.mxRight = parseInt(this.options.mxRight);        this.mxTop = parseInt(this.options.mxTop);        this.mxBottom = parseInt(this.options.mxBottom);        this.LockX = !!this.options.LockX;        this.LockY = !!this.options.LockY;        this.Lock = !!this.options.Lock;        this.onStart = this.options.onStart;        this.onMove = this.options.onMove;        this.onStop = this.options.onStop;        this._Handle = Drag_$(this.options.Handle) || this.Drag;        this._mxContainer = Drag_$(this.options.mxContainer) || null;        this.Drag.style.position = "absolute";        //透明        if (Drag_isIE && !!this.options.Transparent) {            //填充拖放对象            with (this._Handle.appendChild(document.createElement("div")).style) {                width = height = "100%";                backgroundColor = "#fff";                filter = "alpha(opacity:0)";            }        }        addEventHandler(this._Handle, "mousedown", Drag_BindAsEventListener(this, this.Start));    },    //设置默认属性    SetOptions : function(options) {        this.options = {//默认值            Handle : "", //设置触发对象(不设置则使用拖放对象)            Limit : false, //是否设置范围限制(为true时下面参数有用,可以是负数)            mxLeft : 0, //左边限制            mxRight : 9999, //右边限制            mxTop : 0, //上边限制            mxBottom : 9999, //下边限制            mxContainer : "", //指定限制在容器内            LockX : false, //是否锁定水平方向拖放            LockY : false, //是否锁定垂直方向拖放            Lock : false, //是否锁定            Transparent : false, //是否透明            onStart : function() {            }, //开始移动时执行            onMove : function() {            }, //移动时执行            onStop : function() {            }//结束移动时执行        };        Drag_Extend(this.options, options || {});    },    //准备拖动    Start : function(oEvent) {        if (this.Lock) {            return;        }        if (this.Limit) {            //修正错误范围参数            this.mxRight = Math.max(this.mxRight, this.mxLeft + this.Drag.offsetWidth);            this.mxBottom = Math.max(this.mxBottom, this.mxTop + this.Drag.offsetHeight);            //如果有容器必须设置position为relative来相对定位,并在获取offset之前设置            !this._mxContainer || Drag_CurrentStyle(this._mxContainer).position == "relative" || (this._mxContainer.style.position = "relative");        }        //记录鼠标相对拖放对象的位置        this._x = oEvent.clientX - this.Drag.offsetLeft;        this._y = oEvent.clientY - this.Drag.offsetTop;        //记录margin        this._marginLeft = parseInt(Drag_CurrentStyle(this.Drag).marginLeft) || 0;        this._marginTop = parseInt(Drag_CurrentStyle(this.Drag).marginTop) || 0;        //mousemove时移动 mouseup时停止        addEventHandler(document, "mousemove", this._fM);        addEventHandler(document, "mouseup", this._fS);        if (Drag_isIE) {            //焦点丢失            addEventHandler(this._Handle, "losecapture", this._fS);            //设置鼠标捕获            this._Handle.setCapture();        } else {            //焦点丢失            addEventHandler(window, "blur", this._fS);            //阻止默认动作            oEvent.preventDefault();        };        //附加程序        this.onStart();    },    //拖动    Move : function(oEvent) {        //判断是否锁定        if (this.Lock) {            this.Stop();            return;        };        //清除选择        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();        //设置移动参数        var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;        //设置范围限制        if (this.Limit) {            //设置范围参数            var mxLeft = this.mxLeft, mxRight = this.mxRight, mxTop = this.mxTop, mxBottom = this.mxBottom;            //如果设置了容器,再修正范围参数            if (!!this._mxContainer) {                mxLeft = Math.max(mxLeft, 0);                mxTop = Math.max(mxTop, 0);                mxRight = Math.min(mxRight, this._mxContainer.clientWidth);                mxBottom = Math.min(mxBottom, this._mxContainer.clientHeight);            };            //修正移动参数            iLeft = Math.max(Math.min(iLeft, mxRight - this.Drag.offsetWidth), mxLeft);            iTop = Math.max(Math.min(iTop, mxBottom - this.Drag.offsetHeight), mxTop);        }        //设置位置,并修正margin        if (!this.LockX) {            this.Drag.style.left = iLeft - this._marginLeft + "px";        }        if (!this.LockY) {            this.Drag.style.top = iTop - this._marginTop + "px";        }        //附加程序        this.onMove();    },    //停止拖动    Stop : function() {        //移除事件        removeEventHandler(document, "mousemove", this._fM);        removeEventHandler(document, "mouseup", this._fS);        if (Drag_isIE) {            removeEventHandler(this._Handle, "losecapture", this._fS);            this._Handle.releaseCapture();        } else {            removeEventHandler(window, "blur", this._fS);        };        //附加程序        this.onStop();    }};

 

 

HTML文件

 

拖放状态:未开始

 

转载于:https://my.oschina.net/parker/blog/143938

你可能感兴趣的文章
ELK实战之logstash部署及基本语法
查看>>
帧中继环境下ospf的使用(点到点模式)
查看>>
BeanShell变量和方法的作用域
查看>>
LINUX下防恶意扫描软件PortSentry
查看>>
由数据库对sql的执行说JDBC的Statement和PreparedStatement
查看>>
springmvc+swagger2
查看>>
软件评测-信息安全-应用安全-资源控制-用户登录限制(上)
查看>>
cacti集成
查看>>
Android中的Cursor
查看>>
我的友情链接
查看>>
Java Web Application 自架构 一 注解化配置
查看>>
如何 debug Proxy.pac文件
查看>>
Python 学习笔记 - 面向对象(特殊成员)
查看>>
Kubernetes 1.11 手动安装并启用ipvs
查看>>
Puppet 配置管理工具安装
查看>>
Bug多,也别乱来,别被Bug主导了开发
查看>>
sed 替换基础使用
查看>>
高性能的MySQL(5)创建高性能的索引一B-Tree索引
查看>>
附件3:eclipse memory analyze使用教程
查看>>
oracle备份与恢复--rman
查看>>