55 * e events
66 **/
77import helper from './helper' ;
8+
89let drags ;
910let stopD ;
1011const isMobile = helper . isMobile ;
12+ const W = document . body . offsetWidth ;
13+ const H = document . body . offsetHeight ;
1114export default class Resize {
1215 constructor ( $el , $container , ratio , e ) {
1316 e . stopPropagation ( ) ;
@@ -22,113 +25,91 @@ export default class Resize {
2225 this . splitY = ratio . split ( ':' ) [ 1 ] ;
2326 this . el = $el ;
2427 this . container = $container ;
25- drags = function ( e ) {
26- self . drag ( e ) ;
28+ drags = function ( ev ) {
29+ self . drag ( ev ) ;
2730 } ;
28- stopD = function ( e ) {
29- self . stopDrag ( e ) ;
30- }
31- if ( isMobile ) {
31+ stopD = function ( ev ) {
32+ self . stopDrag ( ev ) ;
33+ } ;
34+ if ( isMobile ) {
3235 document . addEventListener ( 'touchmove' , drags , false ) ;
33- document . addEventListener ( 'touchend' , stopD , false )
36+ document . addEventListener ( 'touchend' , stopD , false ) ;
3437 }
3538 document . addEventListener ( 'mousemove' , drags , false ) ;
3639 document . addEventListener ( 'mouseup' , stopD , false ) ;
37-
3840 }
3941
4042 drag ( e ) {
41- if ( ! this . el ) {
43+ if ( ! this . el ) {
4244 return ;
4345 }
44- let $dotBox = document . querySelector ( '.g-crop-image-principal' ) ;
45- let $topH = document . querySelector ( '.info-aside' ) ;
46- let $halfX = parseInt ( document . body . offsetWidth ) - parseInt ( window . getComputedStyle ( $dotBox ) . width ) ;
47- let $halfY = parseInt ( document . body . offsetHeight ) - parseInt ( window . getComputedStyle ( $dotBox ) . height ) - parseInt ( window . getComputedStyle ( $topH ) . height ) ;
48- let resetX = isMobile ? e . changedTouches [ 0 ] [ 'clientX' ] : e . clientX ;
49- let resetY = isMobile ? e . changedTouches [ 0 ] [ 'clientY' ] : e . clientY ;
46+ const $dotBox = document . querySelector ( '.g-crop-image-principal' ) ;
47+ const dotBoxW = window . getComputedStyle ( $dotBox ) . width ;
48+ const dotBoxH = window . getComputedStyle ( $dotBox ) . height ;
49+ const $topH = document . querySelector ( '.info-aside' ) ;
50+ const $halfX = W - dotBoxW ;
51+ const $halfY = H - dotBoxH - window . getComputedStyle ( $topH ) . height ;
52+ const resetX = isMobile ? e . changedTouches [ 0 ] [ 'clientX' ] : e . clientX ;
53+ const resetY = isMobile ? e . changedTouches [ 0 ] [ 'clientY' ] : e . clientY ;
5054
5155 if ( this . splitX > this . splitY ) {
52- if ( parseInt ( resetX ) >= ( $halfX / 2 ) + parseInt ( window . getComputedStyle ( $dotBox ) . width ) ) {
53- return ;
54- } else {
55- if ( parseInt ( this . el . offsetWidth ) >= parseInt ( window . getComputedStyle ( $dotBox ) . width ) ) {
56+ if ( parseInt ( resetX ) <= ( $halfX / 2 ) + dotBoxW ) {
57+ if ( parseInt ( this . el . offsetWidth ) >= dotBoxW ) {
5658 this . el . style . width = window . getComputedStyle ( $dotBox ) . width ;
57- } ;
58- this . el . style . width = ( this . coor . w + ( isMobile ? e . changedTouches [ 0 ] [ 'clientX' ] : e . clientX ) - this . coor . x ) + 'px' ;
59+ }
60+ this . el . style . width = ( this . coor . w + ( isMobile ? e . changedTouches [ 0 ] [ 'clientX' ] : e . clientX ) - this . coor . x ) + 'px' ;
5961 this . el . style . height = parseInt ( this . el . offsetWidth ) * ( this . splitY / this . splitX ) + 'px' ;
60- //限制拖拉的范围在图片内
61- if ( parseInt ( window . getComputedStyle ( $dotBox ) . width ) > parseInt ( window . getComputedStyle ( $dotBox ) . height ) ) {
62+ if ( dotBoxW > dotBoxH ) {
6263 if ( parseInt ( this . el . offsetWidth ) >= parseInt ( window . getComputedStyle ( $dotBox ) . height ) ) {
6364 this . el . style . height = window . getComputedStyle ( $dotBox ) . height ;
6465 this . el . style . width = parseInt ( window . getComputedStyle ( $dotBox ) . height ) * ( this . splitX / this . splitY ) + 'px' ;
65- return ;
66- } ;
67- } else if ( parseInt ( window . getComputedStyle ( $dotBox ) . width ) < parseInt ( window . getComputedStyle ( $dotBox ) . height ) ) {
68- if ( parseInt ( this . el . offsetWidth ) >= parseInt ( window . getComputedStyle ( $dotBox ) . width ) ) {
69- this . el . style . width = window . getComputedStyle ( $dotBox ) . width ;
70- this . el . style . height = parseInt ( window . getComputedStyle ( $dotBox ) . width ) * ( this . splitY / this . splitX ) + 'px' ;
71- return ;
7266 }
73- } else {
74- if ( parseInt ( this . el . offsetHeight ) >= parseInt ( window . getComputedStyle ( $dotBox ) . width ) ) {
67+ } else if ( dotBoxW < dotBoxH ) {
68+ if ( parseInt ( this . el . offsetWidth ) >= dotBoxW ) {
7569 this . el . style . width = window . getComputedStyle ( $dotBox ) . width ;
76- this . el . style . height = parseInt ( window . getComputedStyle ( $dotBox ) . width ) * ( this . splitY / this . splitX ) + 'px' ;
77- return ;
70+ this . el . style . height = dotBoxW * ( this . splitY / this . splitX ) + 'px' ;
7871 }
72+ } else if ( parseInt ( this . el . offsetHeight ) >= dotBoxW ) {
73+ this . el . style . width = dotBoxW ;
74+ this . el . style . height = dotBoxW * ( this . splitY / this . splitX ) + 'px' ;
7975 }
8076 }
81- } else if ( this . splitX < this . splitY ) {
82- if ( parseInt ( resetY ) >= ( $halfY / 2 ) + parseInt ( window . getComputedStyle ( $dotBox ) . height ) + parseInt ( window . getComputedStyle ( $topH ) . height ) ) {
83- return ;
84- } else {
85- this . el . style . height = ( this . coor . h + ( isMobile ? e . changedTouches [ 0 ] [ 'clientY' ] :e . clientY ) - this . coor . y ) + 'px' ;
77+ } else if ( this . splitX < this . splitY ) {
78+ if ( parseInt ( resetY ) <= ( $halfY / 2 ) + dotBoxH + window . getComputedStyle ( $topH ) . height ) {
79+ this . el . style . height = ( this . coor . h + ( isMobile ? e . changedTouches [ 0 ] [ 'clientY' ] : e . clientY ) - this . coor . y ) + 'px' ;
8680 this . el . style . width = parseInt ( this . el . style . height ) * ( this . splitX / this . splitY ) + 'px' ;
87- //限制拖拉的范围在图片内
88- if ( parseInt ( window . getComputedStyle ( $dotBox ) . width ) > parseInt ( window . getComputedStyle ( $dotBox ) . height ) ) {
89- if ( parseInt ( this . el . offsetHeight ) >= parseInt ( window . getComputedStyle ( $dotBox ) . height ) ) {
90- this . el . style . height = window . getComputedStyle ( $dotBox ) . height ;
91- this . el . style . width = parseInt ( window . getComputedStyle ( $dotBox ) . height ) * ( this . splitX / this . splitY ) + 'px' ;
92- return ;
93- } ;
94- } else if ( parseInt ( window . getComputedStyle ( $dotBox ) . width ) < parseInt ( window . getComputedStyle ( $dotBox ) . height ) ) {
95- if ( parseInt ( this . el . offsetWidth ) >= parseInt ( window . getComputedStyle ( $dotBox ) . width ) ) {
96- this . el . style . width = window . getComputedStyle ( $dotBox ) . width ;
97- this . el . style . height = parseInt ( window . getComputedStyle ( $dotBox ) . width ) * ( this . splitY / this . splitX ) + 'px' ;
98- return ;
81+ // 限制拖拉的范围在图片内
82+ if ( dotBoxW > dotBoxH ) {
83+ if ( parseInt ( this . el . offsetHeight ) >= dotBoxH ) {
84+ this . el . style . height = dotBoxH ;
85+ this . el . style . width = dotBoxH * ( this . splitX / this . splitY ) + 'px' ;
9986 }
100- } else {
101- if ( parseInt ( this . el . offsetWidth ) >= parseInt ( window . getComputedStyle ( $dotBox ) . width ) ) {
102- this . el . style . width = window . getComputedStyle ( $dotBox ) . width ;
103- this . el . style . height = parseInt ( window . getComputedStyle ( $dotBox ) . width ) * ( this . splitY / this . splitX ) + 'px' ;
104- return ;
87+ } else if ( dotBoxW < dotBoxH ) {
88+ if ( parseInt ( this . el . offsetWidth ) >= dotBoxW ) {
89+ this . el . style . width = dotBoxW ;
90+ this . el . style . height = dotBoxW * ( this . splitY / this . splitX ) + 'px' ;
10591 }
92+ } else if ( parseInt ( this . el . offsetWidth ) >= dotBoxW ) {
93+ this . el . style . width = dotBoxW ;
94+ this . el . style . height = dotBoxW * ( this . splitY / this . splitX ) + 'px' ;
10695 }
10796 }
108- } else {
109- if ( parseInt ( resetX ) >= ( $halfX / 2 ) + parseInt ( window . getComputedStyle ( $dotBox ) . width ) ) {
110- //现在拖拉范围不准超过某一边的范围
111- return ;
112- } else {
113-
114- this . el . style . width = ( this . coor . w + ( isMobile ? e . changedTouches [ 0 ] [ 'clientX' ] :e . clientX ) - this . coor . x ) + 'px' ;
115- this . el . style . height = this . el . style . width ;
116- //限制拖拉的范围在图片内
117- if ( parseInt ( window . getComputedStyle ( $dotBox ) . width ) > parseInt ( window . getComputedStyle ( $dotBox ) . height ) ) {
118- if ( parseInt ( this . el . offsetHeight ) >= parseInt ( window . getComputedStyle ( $dotBox ) . height ) ) {
119- this . el . style . height = window . getComputedStyle ( $dotBox ) . height ;
120- this . el . style . width = window . getComputedStyle ( $dotBox ) . height ;
121- } ;
122- } else if ( parseInt ( window . getComputedStyle ( $dotBox ) . width ) < parseInt ( window . getComputedStyle ( $dotBox ) . height ) ) {
123- if ( parseInt ( this . el . offsetWidth ) >= parseInt ( window . getComputedStyle ( $dotBox ) . width ) ) {
124- this . el . style . width = window . getComputedStyle ( $dotBox ) . width ;
125- this . el . style . height = window . getComputedStyle ( $dotBox ) . width ;
126- }
127- } else {
128- if ( parseInt ( this . el . offsetWidth ) >= parseInt ( window . getComputedStyle ( $dotBox ) . width ) ) {
129- this . el . style . width = this . el . style . height = window . getComputedStyle ( $dotBox ) . width ;
130- }
97+ } else if ( parseInt ( resetX ) <= ( $halfX / 2 ) + dotBoxW ) {
98+ this . el . style . width = ( this . coor . w + ( isMobile ? e . changedTouches [ 0 ] [ 'clientX' ] : e . clientX ) - this . coor . x ) + 'px' ;
99+ this . el . style . height = this . el . style . width ;
100+ // 限制拖拉的范围在图片内
101+ if ( dotBoxW > dotBoxH ) {
102+ if ( parseInt ( this . el . offsetHeight ) >= dotBoxH ) {
103+ this . el . style . height = dotBoxH ;
104+ this . el . style . width = dotBoxH ;
105+ }
106+ } else if ( dotBoxW < dotBoxH ) {
107+ if ( parseInt ( this . el . offsetWidth ) >= dotBoxW ) {
108+ this . el . style . width = dotBoxW ;
109+ this . el . style . height = dotBoxW ;
131110 }
111+ } else if ( parseInt ( this . el . offsetWidth ) >= dotBoxW ) {
112+ this . el . style . width = this . el . style . height = dotBoxW ;
132113 }
133114 }
134115 // if(isMobile) {
@@ -138,14 +119,13 @@ export default class Resize {
138119 // }
139120 }
140121
141- stopDrag ( e ) {
122+ stopDrag ( ) {
142123 this . el = null ;
143- if ( isMobile ) {
144- document . removeEventListener ( 'touchmove' , drags , false ) ;
145- document . removeEventListener ( 'touchend' , stopD , false ) ;
124+ if ( isMobile ) {
125+ document . removeEventListener ( 'touchmove' , drags , false ) ;
126+ document . removeEventListener ( 'touchend' , stopD , false ) ;
146127 }
147- document . removeEventListener ( 'mousemove' , drags , false ) ;
148- document . removeEventListener ( 'mouseup' , stopD , false ) ;
149- //this.container = null;
128+ document . removeEventListener ( 'mousemove' , drags , false ) ;
129+ document . removeEventListener ( 'mouseup' , stopD , false ) ;
150130 }
151- } ;
131+ }
0 commit comments