drag.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. (function($) {
  2. $.fn.drags = function(opt) {
  3. opt = $.extend({handle:"",cursor:"move"}, opt);
  4. if(opt.handle === "") {
  5. var $el = this;
  6. } else {
  7. var $el = this.find(opt.handle);
  8. }
  9. return $el.css('cursor', opt.cursor).on("mousedown", function(e) {
  10. if(opt.handle === "") {
  11. var $drag = $(this).addClass('draggable');
  12. } else {
  13. var $drag = $(this).addClass('active-handle').parent().addClass('draggable');
  14. }
  15. var z_idx = $drag.css('z-index'),
  16. drg_h = $drag.outerHeight(),
  17. drg_w = $drag.outerWidth(),
  18. pos_y = $drag.offset().top + drg_h - e.pageY,
  19. pos_x = $drag.offset().left + drg_w - e.pageX;
  20. $drag.css('z-index', 1000).parents().on("mousemove", function(e) {
  21. $('.draggable').offset({
  22. top:e.pageY + pos_y - drg_h,
  23. left:e.pageX + pos_x - drg_w
  24. }).on("mouseup", function() {
  25. $(this).removeClass('draggable').css('z-index', z_idx);
  26. });
  27. });
  28. e.preventDefault(); // disable selection
  29. }).on("mouseup", function() {
  30. if(opt.handle === "") {
  31. $(this).removeClass('draggable');
  32. } else {
  33. $(this).removeClass('active-handle').parent().removeClass('draggable');
  34. }
  35. });
  36. }
  37. })(jQuery);