簡単なドラッグ&ドロップコントロール


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ファイル
 
<style>
    #idContainer {
        border: 10px solid #990000;
        width: 600px;
        height: 300px;
    }
    #idDrag {
        border: 5px solid #C4E3FD;
        background: #C4E3FD;
        width: 50px;
        height: 50px;
        margin: 50px;
    }
    #idHandle {
        cursor: move;
        height: 25px;
        background: #0000FF;
        overflow: hidden;
    }
</style>
<div id="idContainer">
    <div id="idDrag">
        <div id="idHandle">
            
        </div>
    </div>
</div>
<input id="idReset" type="button" value="  " />
<input id="idLock" type="button" value="  " />
<input id="idLockX" type="button" value="    " />
<input id="idLockY" type="button" value="    " />
<input id="idLimit" type="button" value="    " />
<input id="idLimitOff" type="button" value="    " />
<br />
    :<span id="idShow">   </span>
<script>
    var drag = new Drag("idDrag", {
        mxContainer : "idContainer",
        Handle : "idHandle",
        Limit : true,
        onStart : function() {
            Drag_$("idShow").innerHTML = "    ";
        },
        onMove : function() {
            Drag_$("idShow").innerHTML = "left:" + this.Drag.offsetLeft + ";top:" + this.Drag.offsetTop;
        },
        onStop : function() {
            Drag_$("idShow").innerHTML = "    ";
        }
    });
    Drag_$("idReset").onclick = function() {
        drag.Limit = true;
        drag.mxLeft = drag.mxTop = 0;
        drag.mxRight = drag.mxBottom = 9999;
        drag.LockX = drag.LockY = drag.Lock = false;
    }
    Drag_$("idLock").onclick = function() {
        drag.Lock = true;
    }
    Drag_$("idLockX").onclick = function() {
        drag.LockX = true;
    }
    Drag_$("idLockY").onclick = function() {
        drag.LockY = true;
    }
    Drag_$("idLimit").onclick = function() {
        drag.mxRight = drag.mxBottom = 200;
    }
    Drag_$("idLimitOff").onclick = function() {
        drag.Limit = false;
    }
</script>