更新 JayChou-Wiki 时从各处收集了很多历史图片资源,VuePress 的是基于 MarkDown 文件生成的页面,所以支持 md 的插入图片语法和 html 标签。因为用了 hope 主题,内置了图片预览插件,实现了以下交互效果:
- 左右滑动按顺序浏览页面内其他的图片
- 查看图片的描述
- 对图片进行缩放
- 全屏浏览图片
- 下载图片
- 分享图片
但无法实现图片的左右滑动,当单个页面需要引用大量图片时,即便每张图片都单独设置了尺寸,也会显得很杂乱。找了很久解决方案,在 hope 页面的源码仓库中发现作者本人是直接写的 html 平铺效果。

但是每个页面都单独写 html 也太麻烦了,所以使用 AiAgent(忘记是阿里通义灵码还是腾讯 CodeBuddy 了,反正这种小任务都差不多) 在 src.vuepress\styles\index.scss 添加了自定义全局样式,最终实现效果如下:

// 图片滑动组件样式
.image-scroll-container {
  width: 100%;
  overflow: hidden;
  position: relative;
  margin: 1rem 0;
}
.image-scroll-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 1rem 0;
}
.image-scroll-content {
  display: flex;
  gap: 1rem;
  padding: 0 1rem;
  min-width: max-content;
}
.image-scroll-content img {
  height: 200px;
  min-width: 300px;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
// 图片说明文本样式
.image-scroll-content figure {
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.image-scroll-content figcaption {
  margin-top: 0.5rem;
  font-size: 0.9rem;
  color: #666;
  text-align: center;
}
@media (max-width: 768px) {
  .image-scroll-content img {
    height: 150px;
    min-width: 200px;
  }
  
  .image-scroll-content figcaption {
    font-size: 0.8rem;
  }
}
使用时直接在页面中添加:
<div class="image-scroll-container">
  <div class="image-scroll-wrapper">
    <div class="image-scroll-content">
        <figure>
            <img src="图片链接" alt="图片标签" />
            <figcaption>图片显示名</figcaption>
        </figure>
    </div>
  </div>
</div>
				

