perf: optimized zoom out image when scroll

This commit is contained in:
XPoet 2022-10-12 10:37:43 +08:00
parent 58941db28c
commit c0eb63d484
1 changed files with 20 additions and 16 deletions

View File

@ -238,6 +238,7 @@ KEEP.initUtils = () => {
zoomInImage() { zoomInImage() {
let SIDE_GAP = 40 let SIDE_GAP = 40
let isZoomIn = false let isZoomIn = false
let curWinScrollY = 0
let selectedImgDom = null let selectedImgDom = null
const imgDomList = document.querySelectorAll('.markdown-body img') const imgDomList = document.querySelectorAll('.markdown-body img')
const zoomInImgMask = document.querySelector('.zoom-in-image-mask') const zoomInImgMask = document.querySelector('.zoom-in-image-mask')
@ -246,10 +247,10 @@ KEEP.initUtils = () => {
const zoomOut = () => { const zoomOut = () => {
if (isZoomIn) { if (isZoomIn) {
isZoomIn = false isZoomIn = false
curWinScrollY = 0
zoomInImg && (zoomInImg.style.transform = `scale(1)`) zoomInImg && (zoomInImg.style.transform = `scale(1)`)
zoomInImgMask && zoomInImgMask.classList.remove('show') zoomInImgMask && zoomInImgMask.classList.remove('show')
const timer = setTimeout(() => { setTimeout(() => {
clearTimeout(timer)
selectedImgDom && selectedImgDom.classList.remove('hide') selectedImgDom && selectedImgDom.classList.remove('hide')
}, 300) }, 300)
} }
@ -262,7 +263,9 @@ KEEP.initUtils = () => {
}) })
document.addEventListener('scroll', () => { document.addEventListener('scroll', () => {
if (isZoomIn && Math.abs(curWinScrollY - window.scrollY) >= 50) {
zoomOut() zoomOut()
}
}) })
} }
@ -281,6 +284,7 @@ KEEP.initUtils = () => {
zoomOutHandle() zoomOutHandle()
imgDomList.forEach((img) => { imgDomList.forEach((img) => {
img.addEventListener('click', () => { img.addEventListener('click', () => {
curWinScrollY = window.scrollY
isZoomIn = !isZoomIn isZoomIn = !isZoomIn
setSideGap() setSideGap()
zoomInImg.setAttribute('src', img.getAttribute('src')) zoomInImg.setAttribute('src', img.getAttribute('src'))