Skip to content

Commit b135286

Browse files
authored
再试试
Signed-off-by: 本新同学 <yuanbenxin@outlook.com>
1 parent 7847f37 commit b135286

1 file changed

Lines changed: 83 additions & 30 deletions

File tree

docs/.vitepress/theme/components/Donate.vue

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,104 @@
2222
import { onMounted } from 'vue'
2323
2424
onMounted(() => {
25-
// ===== 1. 销毁 medium-zoom 防止冲突 =====
26-
if (window.__mediumZoom__) {
27-
try {
28-
window.__mediumZoom__.detach()
29-
console.log('medium-zoom 已销毁')
30-
} catch (err) {
31-
console.warn('medium-zoom 销毁失败', err)
32-
}
33-
} else {
34-
// 没挂到 window 上,直接克隆替换所有 img,移除绑定事件
35-
document.querySelectorAll('img').forEach(img => {
36-
img.replaceWith(img.cloneNode(true))
25+
// 1. 强制禁用 medium-zoom
26+
try {
27+
// 移除 data-zoom 属性
28+
document.querySelectorAll('img[data-zoom]').forEach(img => {
29+
img.removeAttribute('data-zoom')
3730
})
38-
console.log('medium-zoom 事件已清除')
31+
// 移除 medium-zoom 加的事件监听
32+
const imgs = document.querySelectorAll('img')
33+
imgs.forEach(img => {
34+
const clone = img.cloneNode(true)
35+
img.replaceWith(clone) // 用克隆替换原图,相当于移除了所有事件
36+
})
37+
console.log('✅ medium-zoom 已强制禁用')
38+
} catch (err) {
39+
console.warn('❌ 禁用 medium-zoom 失败', err)
3940
}
4041
42+
4143
// ===== 2. 自定义放大逻辑 =====
42-
document.querySelectorAll('.qrcode-wrapper').forEach(el => {
43-
el.addEventListener('click', () => {
44-
const overlay = document.createElement('div')
45-
overlay.className = 'modal-overlay'
44+
<script setup>
45+
// 等待 DOM 渲染完成
46+
import { onMounted } from 'vue'
4647
47-
const modal = document.createElement('div')
48-
modal.className = 'modal-card'
48+
onMounted(() => {
49+
// 1. 强制禁用 medium-zoom
50+
try {
51+
// 移除它加的 data-zoom 属性
52+
document.querySelectorAll('img[data-zoom]').forEach(img => {
53+
img.removeAttribute('data-zoom')
54+
})
55+
// 移除 medium-zoom 加的事件监听
56+
const imgs = document.querySelectorAll('img')
57+
imgs.forEach(img => {
58+
const clone = img.cloneNode(true)
59+
img.replaceWith(clone) // 用克隆替换原图,相当于移除了所有事件
60+
})
61+
console.log('✅ medium-zoom 已强制禁用')
62+
} catch (err) {
63+
console.warn('❌ 禁用 medium-zoom 失败', err)
64+
}
4965
50-
// 强制放大时尺寸一致
51-
modal.innerHTML = `
52-
<img src="${el.querySelector('img').src}" alt="二维码" style="width: 320px; height: auto; max-width: 80vw; max-height: 70vh; object-fit: contain;">
53-
<div class="modal-title">SecRandom团队再次感谢您的支持</div>
54-
<div class="modal-subtitle">点击周围空白关闭</div>
55-
`
66+
// 2. 自定义放大逻辑(让两张图放大时大小一致)
67+
const imgs = document.querySelectorAll('.zoomable')
68+
imgs.forEach(img => {
69+
img.addEventListener('click', () => {
70+
const overlay = document.createElement('div')
71+
overlay.style.position = 'fixed'
72+
overlay.style.top = 0
73+
overlay.style.left = 0
74+
overlay.style.width = '100vw'
75+
overlay.style.height = '100vh'
76+
overlay.style.background = 'rgba(0,0,0,0.8)'
77+
overlay.style.display = 'flex'
78+
overlay.style.alignItems = 'center'
79+
overlay.style.justifyContent = 'center'
80+
overlay.style.zIndex = 9999
81+
82+
const bigImg = document.createElement('img')
83+
bigImg.src = img.src
84+
bigImg.style.maxWidth = '80vw'
85+
bigImg.style.maxHeight = '80vh'
86+
bigImg.style.objectFit = 'contain'
87+
88+
// ✅ 统一大小逻辑
89+
// 先获取两张原图的尺寸,取一个最大宽高作为展示基准
90+
const allImgs = Array.from(document.querySelectorAll('.zoomable'))
91+
let maxW = 0, maxH = 0
92+
allImgs.forEach(i => {
93+
const temp = new Image()
94+
temp.src = i.src
95+
temp.onload = () => {
96+
maxW = Math.max(maxW, temp.width)
97+
maxH = Math.max(maxH, temp.height)
98+
}
99+
})
100+
// 给放大图固定成同一比例的尺寸(缩放到相同的宽高范围)
101+
bigImg.style.width = maxW + 'px'
102+
bigImg.style.height = maxH + 'px'
56103
57-
overlay.appendChild(modal)
104+
overlay.appendChild(bigImg)
58105
document.body.appendChild(overlay)
59106
60-
overlay.addEventListener('click', (e) => {
61-
if (e.target === overlay) {
62-
document.body.removeChild(overlay)
63-
}
107+
overlay.addEventListener('click', () => {
108+
overlay.remove()
64109
})
65110
})
66111
})
67112
})
68113
</script>
69114
115+
<template>
116+
<div>
117+
<img src="/img1.png" class="zoomable" style="width: 300px;" />
118+
<img src="/img2.png" class="zoomable" style="width: 200px;" />
119+
</div>
120+
</template>
121+
122+
70123
<style scoped>
71124
.donate-container {
72125
display: flex;

0 commit comments

Comments
 (0)