jquery.dragmove.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Plugin: jQuery.dragmove
  2. // Source: github.com/nathco/jQuery.dragmove
  3. // Author: Nathan Rutzky
  4. // Update: 1.0
  5. (function($) {
  6. $.fn.dragmove = function(divParent) {
  7. return this.each(function() {
  8. var $document = $(document),
  9. $this = $(this),
  10. active,
  11. startX,
  12. startY;
  13. $this.on('mousedown touchstart', function(e) {
  14. active = true;
  15. startX = e.originalEvent.pageX - $this.offset().left;
  16. startY = e.originalEvent.pageY - $this.offset().top;
  17. if ('mousedown' == e.type)
  18. if(divParent){
  19. click = divParent;
  20. }else {
  21. click = $this;
  22. }
  23. if ('touchstart' == e.type)
  24. if(divParent){
  25. click = divParent;
  26. }else {
  27. touch = $this;
  28. }
  29. if (window.mozInnerScreenX == null)
  30. return false;
  31. });
  32. $document.on('mousemove touchmove', function(e) {
  33. var topZhi = e.originalEvent.pageY - startY;
  34. var screenHeight = document.body.clientHeight || document.documentElement.clientHeight;
  35. if((e.originalEvent.pageY - startY)<0){
  36. topZhi = 0;
  37. }
  38. if(e.originalEvent.pageY>screenHeight){
  39. topZhi = screenHeight-48; //弹窗高度不固定,保留弹窗头部高度
  40. }
  41. if ('mousemove' == e.type && active)
  42. click.offset({
  43. left: e.originalEvent.pageX - startX,
  44. top: topZhi
  45. });
  46. if ('touchmove' == e.type && active)
  47. touch.offset({
  48. left: e.originalEvent.pageX - startX,
  49. top: e.originalEvent.pageY - startY
  50. });
  51. }).on('mouseup touchend', function() {
  52. active = false;
  53. });
  54. });
  55. };
  56. })(jQuery);