Skip to content

Commit ec01735

Browse files
committed
Zoom/drag image
Signed-off-by: Yukai Huang <[email protected]>
1 parent 4219985 commit ec01735

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

public/js/lib/renderer/lightbox/index.js

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import './lightbox.css'
22

33
let images = []
4+
/** @type {HTMLImageElement} */
45
let currentImage = null
56
let currentIndexIndex = 0
67

@@ -21,10 +22,13 @@ function findOrCreateLightboxContainer () {
2122
</div>
2223
`
2324

25+
addImageZoomListener(lightBoxContainer)
26+
2427
const hideContainer = (e) => {
2528
e.stopPropagation()
2629
lightBoxContainer.classList.remove('show')
2730
document.body.classList.remove('no-scroll')
31+
currentImage = null
2832
}
2933

3034
lightBoxContainer.querySelector('.lightbox-control-previous').addEventListener('click', (e) => {
@@ -36,7 +40,7 @@ function findOrCreateLightboxContainer () {
3640
switchImage(1)
3741
})
3842
lightBoxContainer.querySelector('.lightbox-control-close').addEventListener('click', hideContainer)
39-
lightBoxContainer.addEventListener('click', hideContainer)
43+
// lightBoxContainer.addEventListener('click', hideContainer)
4044

4145
document.body.appendChild(lightBoxContainer)
4246
}
@@ -63,7 +67,8 @@ function setImageInner (img, lightBoxContainer) {
6367
const src = img.getAttribute('src')
6468
const alt = img.getAttribute('alt')
6569

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'))
6772
}
6873

6974
function onClickImage (img) {
@@ -86,15 +91,80 @@ function updateLightboxImages () {
8691
}
8792
}
8893

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+
89158
const init = () => {
90159
const markdownBody = document.querySelector('.markdown-body')
91160
if (!markdownBody) {
92161
return
93162
}
94163

95164
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)
98168
e.stopPropagation()
99169
}
100170
})

public/js/lib/renderer/lightbox/lightbox.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
cursor: pointer;
3030
user-select: none;
3131
font-size: 25px;
32+
z-index: 1;
3233
}
3334

3435
.lightbox-container .lightbox-control-previous:hover,
3536
.lightbox-container .lightbox-control-next:hover,
3637
.lightbox-container .lightbox-control-close:hover {
37-
cursor: rgba(130, 130, 130, 0.78);
38+
color: rgba(130, 130, 130, 0.78);
3839
}
3940

4041
.lightbox-container .lightbox-control-next,
@@ -57,6 +58,13 @@
5758

5859
.lightbox-container .lightbox-inner img {
5960
max-width: 100%;
61+
cursor: move;
62+
63+
transform-origin: 0 0;
64+
-moz-transform-origin: 0 0;
65+
-webkit-transform-origin: 0 0;
66+
-ms-transform-origin: 0 0;
67+
-o-transform-origin: 0 0;
6068
}
6169

6270
.markdown-body img.md-image {

0 commit comments

Comments
 (0)