1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;" /> <title>拖放效果</title> </head>
<body>
<script>
var isIE = (document.all) ? true : false;
var $ = function (id) { return "string" == typeof id ? document.getElementById(id) : id; };
var Class = { create: function() { return function() { this.initialize.apply(this, arguments); } } }
var Extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } }
var Bind = function(object, fun) { return function() { return fun.apply(object, arguments); } }
var BindAsEventListener = function(object, fun) { return function(event) { return fun.call(object, (event || window.event)); } }
var 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 = Class.create(); Drag.prototype = { initialize: function(drag, options) { this.Drag = $(drag); this._x = this._y = 0; this._marginLeft = this._marginTop = 0;
this._fM = BindAsEventListener(this, this.Move); this._fS = 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 = $(this.options.Handle) || this.Drag; this._mxContainer = $(this.options.mxContainer) || null; this.Drag.style.position = "absolute";
if(isIE && !!this.options.Transparent){
with(this._Handle.appendChild(document.createElement("div")).style){ width = height = "100%"; backgroundColor = "#fff"; filter = "alpha(opacity:0)"; fontSize = 0; } }
this.Repair(); addEventHandler(this._Handle, "mousedown", BindAsEventListener(this, this.Start)); }, SetOptions: function(options) { this.options = { Handle: "", Limit: false, mxLeft: 0, mxRight: 9999, mxTop: 0, mxBottom: 9999, mxContainer: "", LockX: false, LockY: false, Lock: false, Transparent: false, onStart: function(){}, onMove: function(){}, onStop: function(){} }; Extend(this.options, options || {}); }, Start: function(oEvent) { if(this.Lock){ return; } this.Repair();
this._x = oEvent.clientX - this.Drag.offsetLeft; this._y = oEvent.clientY - this.Drag.offsetTop;
this._marginLeft = parseInt(CurrentStyle(this.Drag).marginLeft) || 0; this._marginTop = parseInt(CurrentStyle(this.Drag).marginTop) || 0;
addEventHandler(document, "mousemove", this._fM); addEventHandler(document, "mouseup", this._fS); if(isIE){
addEventHandler(this._Handle, "losecapture", this._fS);
this._Handle.setCapture(); }else{
addEventHandler(window, "blur", this._fS);
oEvent.preventDefault(); };
this.onStart(); }, Repair: function() { 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);
!this._mxContainer || CurrentStyle(this._mxContainer).position == "relative" || CurrentStyle(this._mxContainer).position == "absolute" || (this._mxContainer.style.position = "relative"); } }, 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); }
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(isIE){ removeEventHandler(this._Handle, "losecapture", this._fS); this._Handle.releaseCapture(); }else{ removeEventHandler(window, "blur", this._fS); };
this.onStop(); } }; </script>
<style> #idContainer{ border:10px solid #990000; width:600px; height:300px;} #idDrag{ border:5px solid #C4E3FD; background:#C4E3FD; width:50px; height:50px; top:50px; left: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="取消范围锁定" />
拖放状态:<span id="idShow">未开始</span>
<script> var drag = new Drag("idDrag", { mxContainer: "idContainer", Handle: "idHandle", Limit: true, onStart: function(){ $("idShow").innerHTML = "开始拖放"; }, onMove: function(){ $("idShow").innerHTML = "left:"+this.Drag.offsetLeft+";top:"+this.Drag.offsetTop; }, onStop: function(){ $("idShow").innerHTML = "结束拖放"; } });
$("idReset").onclick = function(){ drag.Limit = true; drag.mxLeft = drag.mxTop = 0; drag.mxRight = drag.mxBottom = 9999; drag.LockX = drag.LockY = drag.Lock = false; }
$("idLock").onclick = function(){ drag.Lock = true; } $("idLockX").onclick = function(){ drag.LockX = true; } $("idLockY").onclick = function(){ drag.LockY = true; } $("idLimit").onclick = function(){ drag.mxRight = drag.mxBottom = 200;drag.Limit = true; } $("idLimitOff").onclick = function(){ drag.Limit = false; } </script>
</body> </html>
|