drag.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * author levi
  3. * url http://levi.cg.am
  4. */
  5. $(function() {
  6. function debug(msg){
  7. $("#debug").text(msg + "\n");
  8. }
  9. $(document).mousemove(function(e) {
  10. if (!!this.move) {
  11. var posix = !document.move_target ? {'x': 0, 'y': 0} : document.move_target.posix,
  12. callback = document.call_down || function() {
  13. var posTop = e.pageY - posix.y, posLeft = e.pageX - posix.x;
  14. var canvas = $(this.move_target).parent();
  15. if(posTop < 0){
  16. posTop = 0;
  17. }else if(posTop > canvas.innerHeight() - $(this.move_target).height()){
  18. posTop = canvas.innerHeight() - $(this.move_target).height();
  19. }
  20. if(posLeft < 0){
  21. posLeft = 0;
  22. }else if(posLeft > canvas.innerWidth() - $(this.move_target).width()){
  23. posLeft = canvas.innerWidth() - $(this.move_target).width();
  24. }
  25. $(this.move_target).css({
  26. 'top': posTop,
  27. 'left': posLeft
  28. });
  29. };
  30. callback.call(this, e, posix);
  31. return false;
  32. }
  33. }).mouseup(function(e) {
  34. if (!!this.move) {
  35. var callback = document.call_up || function(){};
  36. callback.call(this, e);
  37. $.extend(this, {
  38. 'move': false,
  39. 'move_target': null,
  40. 'call_down': false,
  41. 'call_up': false
  42. });
  43. }
  44. });
  45. });