@@ -54,8 +54,8 @@ export function initImageDiff() {
54
54
} ;
55
55
56
56
return {
57
- image1 : $ ( image1 ) ,
58
- image2 : $ ( image2 ) ,
57
+ $ image1 : $ ( image1 ) ,
58
+ $ image2 : $ ( image2 ) ,
59
59
size1,
60
60
size2,
61
61
max,
@@ -124,18 +124,18 @@ export function initImageDiff() {
124
124
factor = ( diffContainerWidth - 24 ) / 2 / sizes . max . width ;
125
125
}
126
126
127
- const widthChanged = sizes . image1 . length !== 0 && sizes . image2 . length !== 0 && sizes . image1 [ 0 ] . naturalWidth !== sizes . image2 [ 0 ] . naturalWidth ;
128
- const heightChanged = sizes . image1 . length !== 0 && sizes . image2 . length !== 0 && sizes . image1 [ 0 ] . naturalHeight !== sizes . image2 [ 0 ] . naturalHeight ;
129
- if ( sizes . image1 . length !== 0 ) {
130
- $container . find ( '.bounds-info-after .bounds-info-width' ) . text ( `${ sizes . image1 [ 0 ] . naturalWidth } px` ) . addClass ( widthChanged ? 'green' : '' ) ;
131
- $container . find ( '.bounds-info-after .bounds-info-height' ) . text ( `${ sizes . image1 [ 0 ] . naturalHeight } px` ) . addClass ( heightChanged ? 'green' : '' ) ;
127
+ const widthChanged = sizes . $ image1. length !== 0 && sizes . $ image2. length !== 0 && sizes . $ image1[ 0 ] . naturalWidth !== sizes . $ image2[ 0 ] . naturalWidth ;
128
+ const heightChanged = sizes . $ image1. length !== 0 && sizes . $ image2. length !== 0 && sizes . $ image1[ 0 ] . naturalHeight !== sizes . $ image2[ 0 ] . naturalHeight ;
129
+ if ( sizes . $ image1. length !== 0 ) {
130
+ $container . find ( '.bounds-info-after .bounds-info-width' ) . text ( `${ sizes . $ image1[ 0 ] . naturalWidth } px` ) . addClass ( widthChanged ? 'green' : '' ) ;
131
+ $container . find ( '.bounds-info-after .bounds-info-height' ) . text ( `${ sizes . $ image1[ 0 ] . naturalHeight } px` ) . addClass ( heightChanged ? 'green' : '' ) ;
132
132
}
133
- if ( sizes . image2 . length !== 0 ) {
134
- $container . find ( '.bounds-info-before .bounds-info-width' ) . text ( `${ sizes . image2 [ 0 ] . naturalWidth } px` ) . addClass ( widthChanged ? 'red' : '' ) ;
135
- $container . find ( '.bounds-info-before .bounds-info-height' ) . text ( `${ sizes . image2 [ 0 ] . naturalHeight } px` ) . addClass ( heightChanged ? 'red' : '' ) ;
133
+ if ( sizes . $ image2. length !== 0 ) {
134
+ $container . find ( '.bounds-info-before .bounds-info-width' ) . text ( `${ sizes . $ image2[ 0 ] . naturalWidth } px` ) . addClass ( widthChanged ? 'red' : '' ) ;
135
+ $container . find ( '.bounds-info-before .bounds-info-height' ) . text ( `${ sizes . $ image2[ 0 ] . naturalHeight } px` ) . addClass ( heightChanged ? 'red' : '' ) ;
136
136
}
137
137
138
- const image1 = sizes . image1 [ 0 ] ;
138
+ const image1 = sizes . $ image1[ 0 ] ;
139
139
if ( image1 ) {
140
140
const container = image1 . parentNode ;
141
141
image1 . style . width = `${ sizes . size1 . width * factor } px` ;
@@ -145,7 +145,7 @@ export function initImageDiff() {
145
145
container . style . height = `${ sizes . size1 . height * factor + 2 } px` ;
146
146
}
147
147
148
- const image2 = sizes . image2 [ 0 ] ;
148
+ const image2 = sizes . $ image2[ 0 ] ;
149
149
if ( image2 ) {
150
150
const container = image2 . parentNode ;
151
151
image2 . style . width = `${ sizes . size2 . width * factor } px` ;
@@ -162,7 +162,7 @@ export function initImageDiff() {
162
162
factor = ( diffContainerWidth - 12 ) / sizes . max . width ;
163
163
}
164
164
165
- const image1 = sizes . image1 [ 0 ] ;
165
+ const image1 = sizes . $ image1[ 0 ] ;
166
166
if ( image1 ) {
167
167
const container = image1 . parentNode ;
168
168
const swipeFrame = container . parentNode ;
@@ -175,7 +175,7 @@ export function initImageDiff() {
175
175
swipeFrame . style . width = `${ sizes . max . width * factor + 2 } px` ;
176
176
}
177
177
178
- const image2 = sizes . image2 [ 0 ] ;
178
+ const image2 = sizes . $ image2[ 0 ] ;
179
179
if ( image2 ) {
180
180
const container = image2 . parentNode ;
181
181
const swipeFrame = container . parentNode ;
@@ -222,38 +222,39 @@ export function initImageDiff() {
222
222
factor = ( diffContainerWidth - 12 ) / sizes . max . width ;
223
223
}
224
224
225
- sizes . image1 . css ( {
226
- width : sizes . size1 . width * factor ,
227
- height : sizes . size1 . height * factor ,
228
- } ) ;
229
- sizes . image2 . css ( {
230
- width : sizes . size2 . width * factor ,
231
- height : sizes . size2 . height * factor ,
232
- } ) ;
233
- sizes . image1 . parent ( ) . css ( {
234
- margin : `${ sizes . ratio [ 1 ] * factor } px ${ sizes . ratio [ 0 ] * factor } px` ,
235
- width : sizes . size1 . width * factor + 2 ,
236
- height : sizes . size1 . height * factor + 2 ,
237
- } ) ;
238
- sizes . image2 . parent ( ) . css ( {
239
- margin : `${ sizes . ratio [ 3 ] * factor } px ${ sizes . ratio [ 2 ] * factor } px` ,
240
- width : sizes . size2 . width * factor + 2 ,
241
- height : sizes . size2 . height * factor + 2 ,
242
- } ) ;
225
+ const image1 = sizes . $image1 [ 0 ] ;
226
+ if ( image1 ) {
227
+ const container = image1 . parentNode ;
228
+ image1 . style . width = `${ sizes . size1 . width * factor } px` ;
229
+ image1 . style . height = `${ sizes . size1 . height * factor } px` ;
230
+ container . style . margin = `${ sizes . ratio [ 1 ] * factor } px ${ sizes . ratio [ 0 ] * factor } px` ;
231
+ container . style . width = `${ sizes . size1 . width * factor + 2 } px` ;
232
+ container . style . height = `${ sizes . size1 . height * factor + 2 } px` ;
233
+ }
243
234
244
- // some inner elements are `position: absolute`, so the container's height must be large enough
245
- // the "css(width, height)" is somewhat hacky and not easy to understand, it could be improved in the future
246
- sizes . image2 . parent ( ) . parent ( ) . css ( {
247
- width : sizes . max . width * factor + 2 ,
248
- height : sizes . max . height * factor + 2 ,
249
- } ) ;
235
+ const image2 = sizes . $image2 [ 0 ] ;
236
+ if ( image2 ) {
237
+ const container = image2 . parentNode ;
238
+ const overlayFrame = container . parentNode ;
239
+ image2 . style . width = `${ sizes . size2 . width * factor } px` ;
240
+ image2 . style . height = `${ sizes . size2 . height * factor } px` ;
241
+ container . style . margin = `${ sizes . ratio [ 3 ] * factor } px ${ sizes . ratio [ 2 ] * factor } px` ;
242
+ container . style . width = `${ sizes . size2 . width * factor + 2 } px` ;
243
+ container . style . height = `${ sizes . size2 . height * factor + 2 } px` ;
250
244
251
- const $range = $container . find ( "input[type='range']" ) ;
252
- const onInput = ( ) => sizes . image1 . parent ( ) . css ( {
253
- opacity : $range . val ( ) / 100 ,
254
- } ) ;
255
- $range . on ( 'input' , onInput ) ;
256
- onInput ( ) ;
245
+ // some inner elements are `position: absolute`, so the container's height must be large enough
246
+ overlayFrame . style . width = `${ sizes . max . width * factor + 2 } px` ;
247
+ overlayFrame . style . height = `${ sizes . max . height * factor + 2 } px` ;
248
+ }
249
+
250
+ const rangeInput = $container [ 0 ] . querySelector ( 'input[type="range"]' ) ;
251
+ function updateOpacity ( ) {
252
+ if ( sizes ?. $image1 ?. [ 0 ] ) {
253
+ sizes . $image1 [ 0 ] . parentNode . style . opacity = `${ rangeInput . value / 100 } ` ;
254
+ }
255
+ }
256
+ rangeInput ?. addEventListener ( 'input' , updateOpacity ) ;
257
+ updateOpacity ( ) ;
257
258
}
258
259
} ) ;
259
260
}
0 commit comments