chore: 初始化文档站仓库
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/* 文档图片点击全屏查看 */
|
||||
|
||||
/* 正文图片:加边框、圆角与阴影,避免白色截图与正文融在一起 */
|
||||
.vp-doc img {
|
||||
display: block;
|
||||
margin: 16px auto;
|
||||
padding: 6px;
|
||||
border: 1px solid var(--vp-c-divider);
|
||||
border-radius: 10px;
|
||||
background-color: var(--vp-c-bg-soft);
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
/* 可点击的正文图片:鼠标样式与轻微过渡 */
|
||||
.vp-doc img.doc-zoomable {
|
||||
cursor: zoom-in;
|
||||
transition: opacity 0.2s ease, box-shadow 0.2s ease;
|
||||
}
|
||||
|
||||
.vp-doc img.doc-zoomable:hover {
|
||||
opacity: 0.94;
|
||||
box-shadow: 0 4px 18px rgba(0, 0, 0, 0.16);
|
||||
}
|
||||
|
||||
/* 全屏查看遮罩:默认隐藏,激活时铺满视口 */
|
||||
.doc-image-viewer {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 9999;
|
||||
display: none;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: #000000;
|
||||
cursor: zoom-out;
|
||||
}
|
||||
|
||||
.doc-image-viewer.active {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
/* 图片按比例铺满屏幕(contain,保持完整不裁剪) */
|
||||
.doc-image-viewer img {
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
object-fit: contain;
|
||||
cursor: zoom-out;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// 自定义主题:为文档图片启用「点击全屏查看」
|
||||
//
|
||||
// 点击正文(.vp-doc)内的图片后,在全屏遮罩中铺满展示原图;
|
||||
// 再次点击或按 Esc 关闭。路由切换时重新绑定,避免遗漏动态渲染的图片。
|
||||
import { onMounted, watch, nextTick } from 'vue'
|
||||
import { useRoute } from 'vitepress'
|
||||
import DefaultTheme from 'vitepress/theme'
|
||||
import './custom.css'
|
||||
|
||||
// 全屏查看器遮罩元素(全站复用一个)
|
||||
let viewer: HTMLDivElement | null = null
|
||||
|
||||
/** 关闭全屏查看器 */
|
||||
function closeViewer() {
|
||||
viewer?.classList.remove('active')
|
||||
}
|
||||
|
||||
/** 打开全屏查看器并展示指定图片
|
||||
|
||||
* :param src: 图片地址
|
||||
* :param alt: 图片替代文本
|
||||
*/
|
||||
function openViewer(src: string, alt: string) {
|
||||
if (!viewer) return
|
||||
const img = viewer.querySelector('img')
|
||||
if (!img) return
|
||||
img.src = src
|
||||
img.alt = alt
|
||||
viewer.classList.add('active')
|
||||
}
|
||||
|
||||
/** 创建全屏查看器并注册关闭事件 */
|
||||
function ensureViewer() {
|
||||
if (viewer) return
|
||||
viewer = document.createElement('div')
|
||||
viewer.className = 'doc-image-viewer'
|
||||
viewer.innerHTML = '<img alt="" />'
|
||||
// 点击遮罩或图片本身关闭
|
||||
viewer.addEventListener('click', closeViewer)
|
||||
document.addEventListener('keydown', (event) => {
|
||||
if (event.key === 'Escape') closeViewer()
|
||||
})
|
||||
document.body.appendChild(viewer)
|
||||
}
|
||||
|
||||
/** 为正文图片绑定点击全屏事件(幂等) */
|
||||
function bindImages() {
|
||||
const images = document.querySelectorAll<HTMLImageElement>('.vp-doc img')
|
||||
images.forEach((img) => {
|
||||
if (img.dataset.zoomBound === '1') return
|
||||
img.dataset.zoomBound = '1'
|
||||
img.classList.add('doc-zoomable')
|
||||
img.addEventListener('click', () => {
|
||||
openViewer(img.currentSrc || img.src, img.alt)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
extends: DefaultTheme,
|
||||
setup() {
|
||||
const route = useRoute()
|
||||
|
||||
onMounted(() => {
|
||||
ensureViewer()
|
||||
bindImages()
|
||||
})
|
||||
|
||||
// 路由切换后重新绑定(文章内容异步渲染)
|
||||
watch(
|
||||
() => route.path,
|
||||
() => {
|
||||
closeViewer()
|
||||
nextTick(() => bindImages())
|
||||
},
|
||||
)
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user