draggable.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. var isIE = (document.all) ? true : false;
  2. var dobj = function(id) {
  3. return "string" == typeof id ? document.getElementById(id) : id;
  4. };
  5. var Class = {
  6. create: function() {
  7. return function() { this.initialize.apply(this, arguments); }
  8. }
  9. }
  10. var Extend = function(destination, source) {
  11. for (var property in source) {
  12. destination[property] = source[property];
  13. }
  14. }
  15. var Bind = function(object, fun) {
  16. return function() {
  17. return fun.apply(object, arguments);
  18. }
  19. }
  20. var BindAsEventListener = function(object, fun) {
  21. var args = Array.prototype.slice.call(arguments).slice(2);
  22. return function(event) {
  23. return fun.apply(object, [event || window.event].concat(args));
  24. }
  25. }
  26. var CurrentStyle = function(element) {
  27. return element.currentStyle || document.defaultView.getComputedStyle(element, null);
  28. }
  29. function addEventHandler(oTarget, sEventType, fnHandler) {
  30. if (oTarget.addEventListener) {
  31. oTarget.addEventListener(sEventType, fnHandler, false);
  32. } else if (oTarget.attachEvent) {
  33. oTarget.attachEvent("on" + sEventType, fnHandler);
  34. } else {
  35. oTarget["on" + sEventType] = fnHandler;
  36. }
  37. };
  38. function removeEventHandler(oTarget, sEventType, fnHandler) {
  39. if (oTarget.removeEventListener) {
  40. oTarget.removeEventListener(sEventType, fnHandler, false);
  41. } else if (oTarget.detachEvent) {
  42. oTarget.detachEvent("on" + sEventType, fnHandler);
  43. } else {
  44. oTarget["on" + sEventType] = null;
  45. }
  46. };
  47. //缩放程序
  48. var Resize = Class.create();
  49. Resize.prototype = {
  50. //缩放对象
  51. initialize: function(obj, options) {
  52. this._obj = obj.get(0); //缩放对象
  53. this._styleWidth = this._styleHeight = this._styleLeft = this._styleTop = 0; //样式参数
  54. this._sideRight = this._sideDown = this._sideLeft = this._sideUp = 0; //坐标参数
  55. this._fixLeft = this._fixTop = 0; //定位参数
  56. this._scaleLeft = this._scaleTop = 0; //定位坐标
  57. this._mxSet = function() {}; //范围设置程序
  58. this._mxRightWidth = this._mxDownHeight = this._mxUpHeight = this._mxLeftWidth = 0; //范围参数
  59. this._mxScaleWidth = this._mxScaleHeight = 0; //比例范围参数
  60. this._fun = function() {}; //缩放执行程序
  61. //获取边框宽度
  62. var _style = CurrentStyle(this._obj);
  63. this._borderX = (parseInt(_style.borderLeftWidth) || 0) + (parseInt(_style.borderRightWidth) || 0);
  64. this._borderY = (parseInt(_style.borderTopWidth) || 0) + (parseInt(_style.borderBottomWidth) || 0);
  65. //事件对象(用于绑定移除事件)
  66. this._fR = BindAsEventListener(this, this.Resize);
  67. this._fS = Bind(this, this.Stop);
  68. this.SetOptions(options);
  69. //范围限制
  70. this.Max = !!this.options.Max;
  71. this._mxContainer = $(this.options.mxContainer).get(0) || null;
  72. this.mxLeft = Math.round(this.options.mxLeft);
  73. this.mxRight = Math.round(this.options.mxRight);
  74. this.mxTop = Math.round(this.options.mxTop);
  75. this.mxBottom = Math.round(this.options.mxBottom);
  76. //宽高限制
  77. this.Min = !!this.options.Min;
  78. this.minWidth = Math.round(this.options.minWidth);
  79. this.minHeight = Math.round(this.options.minHeight);
  80. //按比例缩放
  81. this.Scale = !!this.options.Scale;
  82. this.Ratio = Math.max(this.options.Ratio, 0);
  83. this.onResize = this.options.onResize;
  84. this._obj.style.position = "absolute";
  85. !this._mxContainer || CurrentStyle(this._mxContainer).position == "relative" || (this._mxContainer.style.position = "relative");
  86. },
  87. //设置默认属性
  88. SetOptions: function(options) {
  89. this.options = { //默认值
  90. Max: false, //是否设置范围限制(为true时下面mx参数有用)
  91. mxContainer: "", //指定限制在容器内
  92. mxLeft: 0, //左边限制
  93. mxRight: 9999, //右边限制
  94. mxTop: 0, //上边限制
  95. mxBottom: 9999, //下边限制
  96. Min: false, //是否最小宽高限制(为true时下面min参数有用)
  97. minWidth: 50, //最小宽度
  98. minHeight: 50, //最小高度
  99. Scale: false, //是否按比例缩放
  100. Ratio: 0, //缩放比例(宽/高)
  101. onResize: function() {} //缩放时执行
  102. };
  103. Extend(this.options, options || {});
  104. },
  105. //设置触发对象
  106. Set: function(resize, side) {
  107. var fun;
  108. if (!resize) return;
  109. //根据方向设置
  110. switch (side.toLowerCase()) {
  111. case "up":
  112. fun = this.Up;
  113. break;
  114. case "down":
  115. fun = this.Down;
  116. break;
  117. case "left":
  118. fun = this.Left;
  119. break;
  120. case "right":
  121. fun = this.Right;
  122. break;
  123. case "left-up":
  124. fun = this.LeftUp;
  125. break;
  126. case "right-up":
  127. fun = this.RightUp;
  128. break;
  129. case "left-down":
  130. fun = this.LeftDown;
  131. break;
  132. case "right-down":
  133. default:
  134. fun = this.RightDown;
  135. };
  136. //设置触发对象
  137. addEventHandler(resize.get(0), "mousedown", BindAsEventListener(this, this.Start, fun));
  138. },
  139. //准备缩放
  140. Start: function(e, fun, touch) {
  141. //防止冒泡(跟拖放配合时设置)
  142. e.stopPropagation ? e.stopPropagation() : (e.cancelBubble = true);
  143. //设置执行程序
  144. this._fun = fun;
  145. //样式参数值
  146. this._styleWidth = this._obj.clientWidth;
  147. this._styleHeight = this._obj.clientHeight;
  148. this._styleLeft = this._obj.offsetLeft;
  149. this._styleTop = this._obj.offsetTop;
  150. //四条边定位坐标
  151. this._sideLeft = e.clientX - this._styleWidth;
  152. this._sideRight = e.clientX + this._styleWidth;
  153. this._sideUp = e.clientY - this._styleHeight;
  154. this._sideDown = e.clientY + this._styleHeight;
  155. //top和left定位参数
  156. this._fixLeft = this._styleLeft + this._styleWidth;
  157. this._fixTop = this._styleTop + this._styleHeight;
  158. //缩放比例
  159. if (this.Scale) {
  160. //设置比例
  161. this.Ratio = Math.max(this.Ratio, 0) || this._styleWidth / this._styleHeight;
  162. //left和top的定位坐标
  163. this._scaleLeft = this._styleLeft + this._styleWidth / 2;
  164. this._scaleTop = this._styleTop + this._styleHeight / 2;
  165. };
  166. //范围限制
  167. if (this.Max) {
  168. //设置范围参数
  169. var mxLeft = this.mxLeft,
  170. mxRight = this.mxRight,
  171. mxTop = this.mxTop,
  172. mxBottom = this.mxBottom;
  173. //如果设置了容器,再修正范围参数
  174. if (!!this._mxContainer) {
  175. mxLeft = Math.max(mxLeft, 0);
  176. mxTop = Math.max(mxTop, 0);
  177. mxRight = Math.min(mxRight, this._mxContainer.clientWidth);
  178. mxBottom = Math.min(mxBottom, this._mxContainer.clientHeight);
  179. };
  180. //根据最小值再修正
  181. mxRight = Math.max(mxRight, mxLeft + (this.Min ? this.minWidth : 0) + this._borderX);
  182. mxBottom = Math.max(mxBottom, mxTop + (this.Min ? this.minHeight : 0) + this._borderY);
  183. //由于转向时要重新设置所以写成function形式
  184. this._mxSet = function() {
  185. this._mxRightWidth = mxRight - this._styleLeft - this._borderX;
  186. this._mxDownHeight = mxBottom - this._styleTop - this._borderY;
  187. this._mxUpHeight = Math.max(this._fixTop - mxTop, this.Min ? this.minHeight : 0);
  188. this._mxLeftWidth = Math.max(this._fixLeft - mxLeft, this.Min ? this.minWidth : 0);
  189. };
  190. this._mxSet();
  191. //有缩放比例下的范围限制
  192. if (this.Scale) {
  193. this._mxScaleWidth = Math.min(this._scaleLeft - mxLeft, mxRight - this._scaleLeft - this._borderX) * 2;
  194. this._mxScaleHeight = Math.min(this._scaleTop - mxTop, mxBottom - this._scaleTop - this._borderY) * 2;
  195. };
  196. };
  197. //mousemove时缩放 mouseup时停止
  198. addEventHandler(document, "mousemove", this._fR);
  199. addEventHandler(document, "mouseup", this._fS);
  200. if (isIE) {
  201. addEventHandler(this._obj, "losecapture", this._fS);
  202. this._obj.setCapture();
  203. } else {
  204. addEventHandler(window, "blur", this._fS);
  205. e.preventDefault();
  206. };
  207. },
  208. //缩放
  209. Resize: function(e) {
  210. //清除选择
  211. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  212. //执行缩放程序
  213. this._fun(e);
  214. //设置样式,变量必须大于等于0否则ie出错
  215. with(this._obj.style) {
  216. width = this._styleWidth + "px";
  217. height = this._styleHeight + "px";
  218. top = this._styleTop + "px";
  219. left = this._styleLeft + "px";
  220. }
  221. //附加程序
  222. this.onResize();
  223. },
  224. //缩放程序
  225. //上
  226. Up: function(e) {
  227. this.RepairY(this._sideDown - e.clientY, this._mxUpHeight);
  228. this.RepairTop();
  229. this.TurnDown(this.Down);
  230. },
  231. //下
  232. Down: function(e) {
  233. this.RepairY(e.clientY - this._sideUp, this._mxDownHeight);
  234. this.TurnUp(this.Up);
  235. },
  236. //右
  237. Right: function(e) {
  238. this.RepairX(e.clientX - this._sideLeft, this._mxRightWidth);
  239. this.TurnLeft(this.Left);
  240. },
  241. //左
  242. Left: function(e) {
  243. this.RepairX(this._sideRight - e.clientX, this._mxLeftWidth);
  244. this.RepairLeft();
  245. this.TurnRight(this.Right);
  246. },
  247. //右下
  248. RightDown: function(e) {
  249. this.RepairAngle(
  250. e.clientX - this._sideLeft, this._mxRightWidth,
  251. e.clientY - this._sideUp, this._mxDownHeight
  252. );
  253. this.TurnLeft(this.LeftDown) || this.Scale || this.TurnUp(this.RightUp);
  254. },
  255. //右上
  256. RightUp: function(e) {
  257. this.RepairAngle(
  258. e.clientX - this._sideLeft, this._mxRightWidth,
  259. this._sideDown - e.clientY, this._mxUpHeight
  260. );
  261. this.RepairTop();
  262. this.TurnLeft(this.LeftUp) || this.Scale || this.TurnDown(this.RightDown);
  263. },
  264. //左下
  265. LeftDown: function(e) {
  266. this.RepairAngle(
  267. this._sideRight - e.clientX, this._mxLeftWidth,
  268. e.clientY - this._sideUp, this._mxDownHeight
  269. );
  270. this.RepairLeft();
  271. this.TurnRight(this.RightDown) || this.Scale || this.TurnUp(this.LeftUp);
  272. },
  273. //左上
  274. LeftUp: function(e) {
  275. this.RepairAngle(
  276. this._sideRight - e.clientX, this._mxLeftWidth,
  277. this._sideDown - e.clientY, this._mxUpHeight
  278. );
  279. this.RepairTop();
  280. this.RepairLeft();
  281. this.TurnRight(this.RightUp) || this.Scale || this.TurnDown(this.LeftDown);
  282. },
  283. //修正程序
  284. //水平方向
  285. RepairX: function(iWidth, mxWidth) {
  286. iWidth = this.RepairWidth(iWidth, mxWidth);
  287. if (this.Scale) {
  288. var iHeight = this.RepairScaleHeight(iWidth);
  289. if (this.Max && iHeight > this._mxScaleHeight) {
  290. iHeight = this._mxScaleHeight;
  291. iWidth = this.RepairScaleWidth(iHeight);
  292. } else if (this.Min && iHeight < this.minHeight) {
  293. var tWidth = this.RepairScaleWidth(this.minHeight);
  294. if (tWidth < mxWidth) {
  295. iHeight = this.minHeight;
  296. iWidth = tWidth;
  297. }
  298. }
  299. this._styleHeight = iHeight;
  300. this._styleTop = this._scaleTop - iHeight / 2;
  301. }
  302. this._styleWidth = iWidth;
  303. },
  304. //垂直方向
  305. RepairY: function(iHeight, mxHeight) {
  306. iHeight = this.RepairHeight(iHeight, mxHeight);
  307. if (this.Scale) {
  308. var iWidth = this.RepairScaleWidth(iHeight);
  309. if (this.Max && iWidth > this._mxScaleWidth) {
  310. iWidth = this._mxScaleWidth;
  311. iHeight = this.RepairScaleHeight(iWidth);
  312. } else if (this.Min && iWidth < this.minWidth) {
  313. var tHeight = this.RepairScaleHeight(this.minWidth);
  314. if (tHeight < mxHeight) {
  315. iWidth = this.minWidth;
  316. iHeight = tHeight;
  317. }
  318. }
  319. this._styleWidth = iWidth;
  320. this._styleLeft = this._scaleLeft - iWidth / 2;
  321. }
  322. this._styleHeight = iHeight;
  323. },
  324. //对角方向
  325. RepairAngle: function(iWidth, mxWidth, iHeight, mxHeight) {
  326. iWidth = this.RepairWidth(iWidth, mxWidth);
  327. if (this.Scale) {
  328. iHeight = this.RepairScaleHeight(iWidth);
  329. if (this.Max && iHeight > mxHeight) {
  330. iHeight = mxHeight;
  331. iWidth = this.RepairScaleWidth(iHeight);
  332. } else if (this.Min && iHeight < this.minHeight) {
  333. var tWidth = this.RepairScaleWidth(this.minHeight);
  334. if (tWidth < mxWidth) {
  335. iHeight = this.minHeight;
  336. iWidth = tWidth;
  337. }
  338. }
  339. } else {
  340. iHeight = this.RepairHeight(iHeight, mxHeight);
  341. }
  342. this._styleWidth = iWidth;
  343. this._styleHeight = iHeight;
  344. },
  345. //top
  346. RepairTop: function() {
  347. this._styleTop = this._fixTop - this._styleHeight;
  348. },
  349. //left
  350. RepairLeft: function() {
  351. this._styleLeft = this._fixLeft - this._styleWidth;
  352. },
  353. //height
  354. RepairHeight: function(iHeight, mxHeight) {
  355. iHeight = Math.min(this.Max ? mxHeight : iHeight, iHeight);
  356. iHeight = Math.max(this.Min ? this.minHeight : iHeight, iHeight, 0);
  357. return iHeight;
  358. },
  359. //width
  360. RepairWidth: function(iWidth, mxWidth) {
  361. iWidth = Math.min(this.Max ? mxWidth : iWidth, iWidth);
  362. iWidth = Math.max(this.Min ? this.minWidth : iWidth, iWidth, 0);
  363. return iWidth;
  364. },
  365. //比例高度
  366. RepairScaleHeight: function(iWidth) {
  367. return Math.max(Math.round((iWidth + this._borderX) / this.Ratio - this._borderY), 0);
  368. },
  369. //比例宽度
  370. RepairScaleWidth: function(iHeight) {
  371. return Math.max(Math.round((iHeight + this._borderY) * this.Ratio - this._borderX), 0);
  372. },
  373. //转向程序
  374. //转右
  375. TurnRight: function(fun) {
  376. if (!(this.Min || this._styleWidth)) {
  377. this._fun = fun;
  378. this._sideLeft = this._sideRight;
  379. this.Max && this._mxSet();
  380. return true;
  381. }
  382. },
  383. //转左
  384. TurnLeft: function(fun) {
  385. if (!(this.Min || this._styleWidth)) {
  386. this._fun = fun;
  387. this._sideRight = this._sideLeft;
  388. this._fixLeft = this._styleLeft;
  389. this.Max && this._mxSet();
  390. return true;
  391. }
  392. },
  393. //转上
  394. TurnUp: function(fun) {
  395. if (!(this.Min || this._styleHeight)) {
  396. this._fun = fun;
  397. this._sideDown = this._sideUp;
  398. this._fixTop = this._styleTop;
  399. this.Max && this._mxSet();
  400. return true;
  401. }
  402. },
  403. //转下
  404. TurnDown: function(fun) {
  405. if (!(this.Min || this._styleHeight)) {
  406. this._fun = fun;
  407. this._sideUp = this._sideDown;
  408. this.Max && this._mxSet();
  409. return true;
  410. }
  411. },
  412. //停止缩放
  413. Stop: function() {
  414. removeEventHandler(document, "mousemove", this._fR);
  415. removeEventHandler(document, "mouseup", this._fS);
  416. if (isIE) {
  417. removeEventHandler(this._obj, "losecapture", this._fS);
  418. this._obj.releaseCapture();
  419. } else {
  420. removeEventHandler(window, "blur", this._fS);
  421. }
  422. }
  423. };
  424. //拖放程序
  425. var Drag = Class.create();
  426. Drag.prototype = {
  427. //拖放对象
  428. initialize: function(drag, options) {
  429. this.Drag = drag.get(0); //拖放对象
  430. this._x = this._y = 0; //记录鼠标相对拖放对象的位置
  431. this._marginLeft = this._marginTop = 0; //记录margin
  432. //事件对象(用于绑定移除事件)
  433. this._fM = BindAsEventListener(this, this.Move);
  434. this._fS = Bind(this, this.Stop);
  435. this.SetOptions(options);
  436. this.Limit = !!this.options.Limit;
  437. this.mxLeft = parseInt(this.options.mxLeft);
  438. this.mxRight = parseInt(this.options.mxRight);
  439. this.mxTop = parseInt(this.options.mxTop);
  440. this.mxBottom = parseInt(this.options.mxBottom);
  441. this.LockX = !!this.options.LockX;
  442. this.LockY = !!this.options.LockY;
  443. this.Lock = !!this.options.Lock;
  444. this.onStart = this.options.onStart;
  445. this.onMove = this.options.onMove;
  446. this.onStop = this.options.onStop;
  447. this._Handle = $(this.options.Handle).get(0) || this.Drag;
  448. this._mxContainer = $(this.options.mxContainer).get(0) || null;
  449. this.Drag.style.position = "absolute";
  450. //透明
  451. if (isIE && !!this.options.Transparent) {
  452. //填充拖放对象
  453. with(this._Handle.appendChild(document.createElement("div")).style) {
  454. width = height = "100%";
  455. backgroundColor = "#fff";
  456. filter = "alpha(opacity:0)";
  457. fontSize = 0;
  458. }
  459. }
  460. //修正范围
  461. this.Repair();
  462. addEventHandler(this._Handle, "mousedown", BindAsEventListener(this, this.Start));
  463. },
  464. //设置默认属性
  465. SetOptions: function(options) {
  466. this.options = { //默认值
  467. Handle: "", //设置触发对象(不设置则使用拖放对象)
  468. Limit: false, //是否设置范围限制(为true时下面参数有用,可以是负数)
  469. mxLeft: 0, //左边限制
  470. mxRight: 9999, //右边限制
  471. mxTop: 0, //上边限制
  472. mxBottom: 9999, //下边限制
  473. mxContainer: "", //指定限制在容器内
  474. LockX: false, //是否锁定水平方向拖放
  475. LockY: false, //是否锁定垂直方向拖放
  476. Lock: false, //是否锁定
  477. Transparent: false, //是否透明
  478. onStart: function() {}, //开始移动时执行
  479. onMove: function() {}, //移动时执行
  480. onStop: function() {} //结束移动时执行
  481. };
  482. Extend(this.options, options || {});
  483. },
  484. //准备拖动
  485. Start: function(oEvent) {
  486. if (this.Lock) { return; }
  487. this.Repair();
  488. //记录鼠标相对拖放对象的位置
  489. this._x = oEvent.clientX - this.Drag.offsetLeft;
  490. this._y = oEvent.clientY - this.Drag.offsetTop;
  491. //记录margin
  492. this._marginLeft = parseInt(CurrentStyle(this.Drag).marginLeft) || 0;
  493. this._marginTop = parseInt(CurrentStyle(this.Drag).marginTop) || 0;
  494. //mousemove时移动 mouseup时停止
  495. addEventHandler(document, "mousemove", this._fM);
  496. addEventHandler(document, "mouseup", this._fS);
  497. if (isIE) {
  498. //焦点丢失
  499. addEventHandler(this._Handle, "losecapture", this._fS);
  500. //设置鼠标捕获
  501. this._Handle.setCapture();
  502. } else {
  503. //焦点丢失
  504. addEventHandler(window, "blur", this._fS);
  505. //阻止默认动作
  506. oEvent.preventDefault();
  507. };
  508. //附加程序
  509. this.onStart();
  510. },
  511. //修正范围
  512. Repair: function() {
  513. if (this.Limit) {
  514. //修正错误范围参数
  515. this.mxRight = Math.max(this.mxRight, this.mxLeft + this.Drag.offsetWidth);
  516. this.mxBottom = Math.max(this.mxBottom, this.mxTop + this.Drag.offsetHeight);
  517. //如果有容器必须设置position为relative或absolute来相对或绝对定位,并在获取offset之前设置
  518. !this._mxContainer || CurrentStyle(this._mxContainer).position == "relative" || CurrentStyle(this._mxContainer).position == "absolute" || (this._mxContainer.style.position = "relative");
  519. }
  520. },
  521. //拖动
  522. Move: function(oEvent) {
  523. //判断是否锁定
  524. if (this.Lock) { this.Stop(); return; };
  525. //清除选择
  526. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  527. //设置移动参数
  528. var iLeft = oEvent.clientX - this._x,
  529. iTop = oEvent.clientY - this._y;
  530. //设置范围限制
  531. if (this.Limit) {
  532. //设置范围参数
  533. var mxLeft = this.mxLeft,
  534. mxRight = this.mxRight,
  535. mxTop = this.mxTop,
  536. mxBottom = this.mxBottom;
  537. //如果设置了容器,再修正范围参数
  538. if (!!this._mxContainer) {
  539. mxLeft = Math.max(mxLeft, 0);
  540. mxTop = Math.max(mxTop, 0);
  541. mxRight = Math.min(mxRight, this._mxContainer.clientWidth);
  542. mxBottom = Math.min(mxBottom, this._mxContainer.clientHeight);
  543. };
  544. //修正移动参数
  545. iLeft = Math.max(Math.min(iLeft, mxRight - this.Drag.offsetWidth), mxLeft);
  546. iTop = Math.max(Math.min(iTop, mxBottom - this.Drag.offsetHeight), mxTop);
  547. }
  548. //设置位置,并修正margin
  549. if (!this.LockX) { this.Drag.style.left = iLeft - this._marginLeft + "px"; }
  550. if (!this.LockY) { this.Drag.style.top = iTop - this._marginTop + "px"; }
  551. //附加程序
  552. this.onMove();
  553. },
  554. //停止拖动
  555. Stop: function() {
  556. //移除事件
  557. removeEventHandler(document, "mousemove", this._fM);
  558. removeEventHandler(document, "mouseup", this._fS);
  559. if (isIE) {
  560. removeEventHandler(this._Handle, "losecapture", this._fS);
  561. this._Handle.releaseCapture();
  562. } else {
  563. removeEventHandler(window, "blur", this._fS);
  564. };
  565. //附加程序
  566. this.onStop();
  567. }
  568. };
  569. function dragEvent(obj, dragCall, sizeCall) {
  570. obj.append('<div class="corner dRightDown"></div>');
  571. obj.append('<div class="corner dLeftDown"></div>');
  572. obj.append('<div class="corner dRightUp"></div>');
  573. obj.append('<div class="corner dLeftUp"></div>');
  574. obj.append('<div class="corner dRight"></div>');
  575. obj.append('<div class="corner dLeft"></div>');
  576. obj.append('<div class="corner rUp"></div>');
  577. obj.append('<div class="corner rDown"></div>');
  578. var posterrs = new Resize(obj, {
  579. Max: true,
  580. mxContainer: "#view-box",
  581. onResize: sizeCall ? sizeCall : function() {}
  582. });
  583. posterrs.Set($(".dRightDown", obj), "right-down");
  584. posterrs.Set($(".dLeftDown", obj), "left-down");
  585. posterrs.Set($(".dRightUp", obj), "right-up");
  586. posterrs.Set($(".dLeftUp", obj), "left-up");
  587. posterrs.Set($(".dRight", obj), "right");
  588. posterrs.Set($(".dLeft", obj), "left");
  589. posterrs.Set($(".rUp", obj), "up");
  590. posterrs.Set($(".rDown", obj), "down");
  591. posterrs.Scale = true;
  592. new Drag(obj, {
  593. Limit: true,
  594. mxContainer: "#view-box",
  595. onStop: dragCall ? dragCall : function() {}
  596. });
  597. }
粤ICP备19079148号