2026-06-14 20:27:01
在Marked.js渲染环境下实现Markdown图片点击放大功能,可通过以下步骤完成:
为图片添加可点击的交互提示(光标变为放大镜图标):
.blogMarkDown img { cursor: zoom-in;}在Vue的computed属性中完成Markdown渲染后,通过异步操作确保DOM完全构建,再为图片绑定点击事件:
const markdownToHtml = computed(() => { const html = marked.parse(blogContent.value); // 异步等待DOM更新完成 nextTick(() => { buildImgFormat(); }); return html;});async function buildImgFormat() { // 使用nextTick替代sleep(1000),更精准等待DOM更新 await nextTick(); const imgList = document.getElementsByTagName("img"); for (let i = 0; i < imgList.length; i++) { // 过滤非文档图片(根据实际需求调整条件) if (imgList[i].classList.length > 0) continue; imgList[i].onclick = function () { // 更新响应式数据以控制弹窗 picSrc.value = this.src; // 计算图片显示尺寸(限制最大宽高为视口的80%) const ratio = this.width / this.height; let picH = 0.8 * document.documentElement.clientHeight; let picW = picH * ratio; if (picW > 0.8 * document.documentElement.clientWidth) { picW = 0.8 * document.documentElement.clientWidth; picH = picW / ratio; } picHeight.value = `${Math.round(picH)}px`; picWidth.value = `${Math.round(picW)}px`; picShow.value = true; // 显示弹窗 }; }}异步等待:使用nextTick()替代sleep(1000),更高效且精准等待Vue完成DOM更新。
图片过滤:通过classList.length判断排除已添加全局类的图片(需根据实际项目调整条件)。
尺寸计算:根据图片原始宽高比及视口尺寸,动态计算弹窗内图片的显示尺寸,确保不超过视口的80%。
在Vue模板中添加弹窗,绑定响应式数据控制显示与样式:
<template> <div> <div v-html="markdownToHtml" class="blogMarkDown"></div> <!-- 图片放大弹窗 --> <div v-if="picShow" class="pic-modal" @click="picShow = false"> <img :src="picSrc" :style="{ width: picWidth, height: picHeight }" /> </div> </div></template><script>import { ref, computed, nextTick } from 'vue';import marked from 'marked';import hljs from 'highlight.js';export default { setup() { const blogContent = ref(''); // Markdown内容 const picSrc = ref(''); const picWidth = ref(''); const picHeight = ref(''); const picShow = ref(false); // 配置marked.js marked.use(markedHighlight({ langPrefix: 'hljs language-', highlight(code, lang) { const language = hljs.getLanguage(lang) ? lang : 'plaintext'; return hljs.highlight(code, { language }).value; } })); const markdownToHtml = computed(() => { const html = marked.parse(blogContent.value); nextTick(() => { buildImgFormat(); }); return html; }); async function buildImgFormat() { await nextTick(); const imgList = document.getElementsByTagName("img"); for (let i = 0; i < imgList.length; i++) { if (imgList[i].classList.length > 0) continue; imgList[i].onclick = function () { picSrc.value = this.src; const ratio = this.width / this.height; let picH = 0.8 * document.documentElement.clientHeight; let picW = picH * ratio; if (picW > 0.8 * document.documentElement.clientWidth) { picW = 0.8 * document.documentElement.clientWidth; picH = picW / ratio; } picHeight.value = `${Math.round(picH)}px`; picWidth.value = `${Math.round(picW)}px`; picShow.value = true; }; } } return { blogContent, markdownToHtml, picSrc, picWidth, picHeight, picShow }; }};</script><style>.pic-modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.8); display: flex; justify-content: center; align-items: center; z-index: 1000;}.pic-modal img { object-fit: contain;}</style>通过以上步骤,即可在Marked.js渲染的Markdown文档中实现图片点击放大功能,且代码结构清晰、可维护性强。