draggable.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * PHPWind util Library
  3. * @Copyright : Copyright 2011, phpwind.com
  4. * @Descript : 拖拽功能组件
  5. * @Author : chaoren1641@gmail.com, wengqianshan@me.com
  6. * @Depend : jquery.js(1.7 or later)
  7. * $Id: jquery.draggable.js 13814 2012-07-12 09:15:56Z chris.chencq $ :
  8. */
  9. ;(function ( $, window, document, undefined ) {
  10. var pluginName = 'draggable';
  11. var defaults = {
  12. handle : '.handle', // 要拖拽的手柄
  13. limit : true
  14. };
  15. var lastMouseX,lastMouseY;
  16. //当前窗口内捕获鼠标操作
  17. function capture(elem) {
  18. elem.setCapture ? elem.setCapture() : window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
  19. }
  20. function release(elem) {
  21. elem.releaseCapture ? elem.releaseCapture() : window.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
  22. }
  23. function getMousePosition(e) {
  24. var posx = 0,
  25. posy = 0,
  26. db = document.body,
  27. dd = document.documentElement,
  28. e = e || window.event;
  29. if (e.pageX || e.pageY) {
  30. posx = e.pageX;
  31. posy = e.pageY;
  32. }
  33. else if (e.clientX || e.clientY) {
  34. posx = e.clientX + db.scrollLeft + dd.scrollLeft;
  35. posy = e.clientY + db.scrollTop + dd.scrollTop;
  36. }
  37. return { 'x': posx, 'y': posy };
  38. }
  39. function Plugin(element,options) {
  40. this.element = element;
  41. this.options = $.extend( {}, defaults, options) ;
  42. this.handle = element.find(options.handle);
  43. this.init();
  44. }
  45. Plugin.prototype.init = function() {
  46. var handle = this.handle,
  47. options = this.options,
  48. element = this.element,
  49. winWidth,
  50. winHeight,
  51. docScrollTop = 0,
  52. docScrollLeft = 0,
  53. POS_X = 0,
  54. POS_Y = 0,
  55. elemHeight = element.outerHeight(),
  56. elemWidth = element.outerWidth();
  57. handle.css({cursor:'move'});
  58. var el = handle[0].setCapture ? handle : $(document);
  59. handle.on('mousedown',function(e) {
  60. if(options.mousedown) {
  61. options.mousedown(element);
  62. }
  63. if($.browser && $.browser.msie){
  64. //设置鼠标捕获
  65. handle[0].setCapture();
  66. }else{
  67. //焦点丢失
  68. //$(window).blur();
  69. //阻止默认动作
  70. e.preventDefault();
  71. };
  72. capture(this);
  73. //获取窗口尺寸和滚动条状态
  74. winWidth = $(window).width();
  75. winHeight = $(window).height();
  76. docScrollTop = $(document).scrollTop();
  77. docScrollLeft = $(document).scrollLeft();
  78. //获取鼠标的初始偏移值
  79. var offset = element.offset();
  80. var mousePostion = getMousePosition(e);
  81. //记录鼠标相对窗口的位置偏移
  82. if(element.css('position') === 'fixed'){
  83. POS_X = mousePostion.x - offset.left + docScrollLeft;
  84. POS_Y = mousePostion.y - offset.top + docScrollTop;
  85. }else{
  86. POS_X = mousePostion.x - offset.left;
  87. POS_Y = mousePostion.y - offset.top;
  88. }
  89. el.on('mousemove',function(e) {
  90. e.preventDefault();
  91. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  92. //获取鼠标位置、窗口目标位置和移动范围
  93. var mousePostion = getMousePosition(e),
  94. mouseY = mousePostion.y,
  95. mouseX = mousePostion.x,
  96. currentLeft = mouseX - POS_X,
  97. currentTop = mouseY - POS_Y;
  98. //限制弹窗可移动范围,默认不超出可视区域
  99. if(options.limit){
  100. var maxLeft = winWidth - elemWidth + docScrollLeft,
  101. maxTop = winHeight - elemHeight + docScrollTop;
  102. if(currentLeft < docScrollLeft){
  103. currentLeft = docScrollLeft;
  104. }
  105. if(currentTop < docScrollTop){
  106. currentTop = docScrollTop;
  107. }
  108. if(currentLeft > maxLeft){
  109. currentLeft = maxLeft;
  110. }
  111. if(currentTop > maxTop){
  112. currentTop = maxTop;
  113. }
  114. }
  115. //设置窗口位置
  116. element.css({ left : currentLeft + "px", top : currentTop + "px" });
  117. if(options.mousemove) {
  118. options.mousemove(element, left, top);
  119. }
  120. }).on('mouseup.d keydown',function (e) {
  121. //ESC停止拖拽
  122. if(e.type === 'keydown' && e.keyCode !== 27) {
  123. return;
  124. }
  125. release(this);
  126. $(el).unbind('mousemove').unbind('mouseup.d');
  127. if(options.mouseup) {
  128. options.mouseup(element);
  129. }
  130. });
  131. });
  132. }
  133. $.fn[pluginName] = Wind[pluginName]= function ( options ) {
  134. return this.each(function () {
  135. if (!$.data(this, 'plugin_' + pluginName)) {
  136. $.data(this, 'plugin_' + pluginName, new Plugin( $(this), options ));
  137. }
  138. });
  139. }
  140. })( jQuery, window, document );