@@ -54,8 +54,8 @@ export function initImageDiff() {
5454 } ;
5555
5656 return {
57- image1 : $ ( image1 ) ,
58- image2 : $ ( image2 ) ,
57+ $ image1 : $ ( image1 ) ,
58+ $ image2 : $ ( image2 ) ,
5959 size1,
6060 size2,
6161 max,
@@ -124,18 +124,18 @@ export function initImageDiff() {
124124 factor = ( diffContainerWidth - 24 ) / 2 / sizes . max . width ;
125125 }
126126
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' : '' ) ;
132132 }
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' : '' ) ;
136136 }
137137
138- const image1 = sizes . image1 [ 0 ] ;
138+ const image1 = sizes . $ image1[ 0 ] ;
139139 if ( image1 ) {
140140 const container = image1 . parentNode ;
141141 image1 . style . width = `${ sizes . size1 . width * factor } px` ;
@@ -145,7 +145,7 @@ export function initImageDiff() {
145145 container . style . height = `${ sizes . size1 . height * factor + 2 } px` ;
146146 }
147147
148- const image2 = sizes . image2 [ 0 ] ;
148+ const image2 = sizes . $ image2[ 0 ] ;
149149 if ( image2 ) {
150150 const container = image2 . parentNode ;
151151 image2 . style . width = `${ sizes . size2 . width * factor } px` ;
@@ -162,7 +162,7 @@ export function initImageDiff() {
162162 factor = ( diffContainerWidth - 12 ) / sizes . max . width ;
163163 }
164164
165- const image1 = sizes . image1 [ 0 ] ;
165+ const image1 = sizes . $ image1[ 0 ] ;
166166 if ( image1 ) {
167167 const container = image1 . parentNode ;
168168 const swipeFrame = container . parentNode ;
@@ -175,7 +175,7 @@ export function initImageDiff() {
175175 swipeFrame . style . width = `${ sizes . max . width * factor + 2 } px` ;
176176 }
177177
178- const image2 = sizes . image2 [ 0 ] ;
178+ const image2 = sizes . $ image2[ 0 ] ;
179179 if ( image2 ) {
180180 const container = image2 . parentNode ;
181181 const swipeFrame = container . parentNode ;
@@ -222,38 +222,39 @@ export function initImageDiff() {
222222 factor = ( diffContainerWidth - 12 ) / sizes . max . width ;
223223 }
224224
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+ }
243234
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` ;
250244
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 ( ) ;
257258 }
258259 } ) ;
259260}
0 commit comments