drag.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * add by yingp
  3. * 组件拖动
  4. */
  5. var Drag = {
  6. o:null,
  7. init:function(o){
  8. o.onmousedown = this.start;
  9. },
  10. start:function(e){
  11. var o;
  12. e = Drag.fixEvent(e);
  13. e.preventDefault && e.preventDefault();
  14. Drag.o = o = this;
  15. o.x = e.clientX - Drag.o.offsetLeft;
  16. o.y = e.clientY - Drag.o.offsetTop;
  17. document.onmousemove = Drag.move;
  18. document.onmouseup = Drag.end;
  19. },
  20. move:function(e){
  21. e = Drag.fixEvent(e);
  22. var oLeft,oTop;
  23. oLeft = e.clientX - Drag.o.x;
  24. oTop = e.clientY - Drag.o.y;
  25. Drag.o.style.left = oLeft + 'px';
  26. Drag.o.style.top = oTop + 'px';
  27. //add by yingp
  28. $(Drag.o).find('font').text('{↑top:' + oTop + ', ←left:' + oLeft + '}');
  29. if(oLeft <= 0 || oTop <= 0){
  30. $.showtip('不要拖出去了啊...', 2000, oTop, oLeft);
  31. Drag.o.style.left = oLeft <= 0 ? 0 : oLeft + 'px';
  32. Drag.o.style.top = oTop <= 0 ? 0 : oTop + 'px';
  33. $(Drag.o).find('font').text('{↑top:' + oTop + ', ←left:' + oLeft + '}');
  34. //var height = $(Drag.o).css('height').replace(/px/g,'');
  35. //var width = $(Drag.o).css('width').replace(/px/g,'');
  36. //console.log('...' + (Number(width) + oLeft) + ',' + (Number(height) + oTop));
  37. return;
  38. }
  39. //end
  40. },
  41. end:function(e){
  42. e = Drag.fixEvent(e);
  43. Drag.o = document.onmousemove = document.onmouseup = null;
  44. },
  45. fixEvent: function(e){
  46. if (!e) {
  47. e = window.event;
  48. e.target = e.srcElement;
  49. e.layerX = e.offsetX;
  50. e.layerY = e.offsetY;
  51. }
  52. return e;
  53. }
  54. };