1
1
import './lightbox.css'
2
2
3
3
let images = [ ]
4
+ /** @type {HTMLImageElement } */
4
5
let currentImage = null
5
6
let currentIndexIndex = 0
6
7
@@ -21,10 +22,13 @@ function findOrCreateLightboxContainer () {
21
22
</div>
22
23
`
23
24
25
+ addImageZoomListener ( lightBoxContainer )
26
+
24
27
const hideContainer = ( e ) => {
25
28
e . stopPropagation ( )
26
29
lightBoxContainer . classList . remove ( 'show' )
27
30
document . body . classList . remove ( 'no-scroll' )
31
+ currentImage = null
28
32
}
29
33
30
34
lightBoxContainer . querySelector ( '.lightbox-control-previous' ) . addEventListener ( 'click' , ( e ) => {
@@ -36,7 +40,7 @@ function findOrCreateLightboxContainer () {
36
40
switchImage ( 1 )
37
41
} )
38
42
lightBoxContainer . querySelector ( '.lightbox-control-close' ) . addEventListener ( 'click' , hideContainer )
39
- lightBoxContainer . addEventListener ( 'click' , hideContainer )
43
+ // lightBoxContainer.addEventListener('click', hideContainer)
40
44
41
45
document . body . appendChild ( lightBoxContainer )
42
46
}
@@ -63,7 +67,8 @@ function setImageInner (img, lightBoxContainer) {
63
67
const src = img . getAttribute ( 'src' )
64
68
const alt = img . getAttribute ( 'alt' )
65
69
66
- lightBoxContainer . querySelector ( '.lightbox-inner' ) . innerHTML = `<img src="${ src } " alt="${ alt } ">`
70
+ lightBoxContainer . querySelector ( '.lightbox-inner' ) . innerHTML = `<img src="${ src } " alt="${ alt } " draggable="false">`
71
+ addImageDragListener ( lightBoxContainer . querySelector ( '.lightbox-inner img' ) )
67
72
}
68
73
69
74
function onClickImage ( img ) {
@@ -86,15 +91,80 @@ function updateLightboxImages () {
86
91
}
87
92
}
88
93
94
+ function addImageZoomListener ( container ) {
95
+ container . addEventListener ( 'wheel' , function ( e ) {
96
+ // normalize scroll position as percentage
97
+ e . preventDefault ( )
98
+
99
+ /** @type {HTMLImageElement } */
100
+ const image = container . querySelector ( 'img' )
101
+
102
+ if ( ! image ) {
103
+ return
104
+ }
105
+
106
+ let scale = image . getBoundingClientRect ( ) . width / image . offsetWidth
107
+ scale += e . deltaY * - 0.01
108
+
109
+ // Restrict scale
110
+ scale = Math . min ( Math . max ( 0.125 , scale ) , 4 )
111
+
112
+ var transformValue = `scale(${ scale } )`
113
+
114
+ image . style . WebkitTransform = transformValue
115
+ image . style . MozTransform = transformValue
116
+ image . style . OTransform = transformValue
117
+ image . style . transform = transformValue
118
+ } )
119
+ }
120
+
121
+ /**
122
+ * @param {HTMLImageElement } image
123
+ */
124
+ function addImageDragListener ( image ) {
125
+ let moved = false
126
+ let pos = [ ]
127
+ image . addEventListener ( 'mousedown' , ( evt ) => {
128
+ moved = true
129
+
130
+ const { left, top } = image . getBoundingClientRect ( )
131
+
132
+ pos = [
133
+ evt . pageX - left ,
134
+ evt . pageY - top
135
+ ]
136
+ } , true )
137
+
138
+ image . addEventListener ( 'mousemove' , ( evt ) => {
139
+ if ( ! moved ) {
140
+ return
141
+ }
142
+
143
+ image . style . left = `${ evt . pageX - pos [ 0 ] } px`
144
+ image . style . top = `${ evt . pageY - pos [ 1 ] } px`
145
+ image . style . position = 'absolute'
146
+ } , true )
147
+
148
+ image . addEventListener ( 'mouseup' , ( ) => {
149
+ moved = false
150
+ pos = [ ]
151
+ } , true )
152
+
153
+ image . addEventListener ( 'mouseout' , ( ) => {
154
+ moved = false
155
+ } )
156
+ }
157
+
89
158
const init = ( ) => {
90
159
const markdownBody = document . querySelector ( '.markdown-body' )
91
160
if ( ! markdownBody ) {
92
161
return
93
162
}
94
163
95
164
markdownBody . addEventListener ( 'click' , function ( e ) {
96
- if ( e . target . nodeName === 'IMG' && e . target . classList . contains ( 'md-image' ) ) {
97
- onClickImage ( e . target )
165
+ const img = e . target
166
+ if ( img . nodeName === 'IMG' && img . classList . contains ( 'md-image' ) ) {
167
+ onClickImage ( img )
98
168
e . stopPropagation ( )
99
169
}
100
170
} )
0 commit comments