|
51 | 51 | },
|
52 | 52 | start: function(e){
|
53 | 53 | var $container = this.$container,
|
54 |
| - offset = $container.offset(); |
| 54 | + offset = $container.offset(), |
| 55 | + $doc = $(document); |
55 | 56 |
|
56 | 57 | this.startArgs = {
|
57 |
| - width: $container.width(), |
58 |
| - height : $container.height(), |
59 | 58 | left: offset.left,
|
60 | 59 | top: offset.top,
|
61 | 60 | x: e.clientX,
|
62 |
| - y: e.clientY |
| 61 | + y: e.clientY, |
| 62 | + maxLeft: $doc.width() - $container.width(), |
| 63 | + maxTop: $doc.height() - $container.height() |
63 | 64 | };
|
64 | 65 |
|
65 | 66 | this.dragable = true;
|
66 | 67 | },
|
67 | 68 | move: function(e){
|
68 |
| - var startArgs = this.startArgs, |
| 69 | + var $doc = $(document), |
| 70 | + startArgs = this.startArgs, |
69 | 71 | offsetX = e.clientX - startArgs.x,
|
70 | 72 | offsetY = e.clientY - startArgs.y,
|
71 | 73 | newLeft = startArgs.left + offsetX,
|
72 |
| - newTop = startArgs.top + offsetY, |
73 |
| - fullWidth = newLeft + startArgs.width, |
74 |
| - fullHeight = newTop + startArgs.height, |
75 |
| - $doc = $(document); |
| 74 | + newTop = startArgs.top + offsetY; |
76 | 75 |
|
77 |
| - if(newLeft + startArgs.width <= $doc.width() && newLeft >= 0) |
78 |
| - this.$container.css('left', newLeft); |
| 76 | + if(newLeft < 0) |
| 77 | + newLeft = 0; |
79 | 78 |
|
80 |
| - if(newTop + startArgs.height <= $doc.height() && newTop >= 0) |
81 |
| - this.$container.css('top', newTop); |
| 79 | + if(newTop < 0) |
| 80 | + newTop = 0; |
82 | 81 |
|
| 82 | + if(newLeft > startArgs.maxLeft) |
| 83 | + newLeft = startArgs.maxLeft; |
| 84 | + |
| 85 | + if(newTop > startArgs.maxTop) |
| 86 | + newTop = startArgs.maxTop; |
| 87 | + |
| 88 | + this.$container.css('left', newLeft); |
| 89 | + this.$container.css('top', newTop); |
83 | 90 | },
|
84 | 91 | destory: function(){
|
85 | 92 | var me = this;
|
|
0 commit comments