您的当前位置:首页>全部文章>文章详情

强大的图片查看器插件Viewer.js,开源中国在用的文章图片查看器

发表于:2022-03-01 15:47:36浏览:1867次TAG: #图片查看器 #Viewer #文章图片查看器

背景:最近在优化博客文章中的图片查看效果,网上发现很多这样的插件,但是很多都是代码比较膨大,而且需要引用好几个文件,还要引用图片按钮之类的,偶然发现开源中国的文章图片的查看效果不错,F12一下,发现是用Viewer插件做的,引用文件也比较少,于是就选用Viewer.js这款插件了。
另外还有一款fancybox也不错的,这里也记录一下。

Viewer.js 有以下特点:

  • 支持移动设备触摸事件
  • 支持响应式
  • 支持放大/缩小
  • 支持旋转(类似微博的图片旋转)
  • 支持水平/垂直翻转
  • 支持图片移动
  • 支持键盘
  • 支持全屏幻灯片模式(可做屏保)
  • 支持缩略图
  • 支持标题显示
  • 支持多种自定义事件

在线演示及下载

在线演示:https://blog.gougucms.com/home/index/detail/id/88.html
github地址:https://github.com/fengyuanchen/viewerjs

使用方法

引入文件:

<link rel="stylesheet" href="css/viewer.min.css">
<script src="js/viewer.min.js"></script>

Html结构

<ul id="viewer">
    <li><img src="img/viewer1.jpg" alt="图片1"></li>
    <li><img src="img/viewer2.jpg" alt="图片2"></li>
    <li><img src="img/viewer3.jpg" alt="图片3"></li>
    <li><img src="img/viewer4.jpg" alt="图片4"></li>
    <li><img src="img/viewer5.jpg" alt="图片5"></li>
    <li><img src="img/viewer6.jpg" alt="图片6"></li>
</ul>
var viewer = new Viewer(document.getElementById('viewer'));

配置

名称 类型 默认值 说明
inline 布尔值 false 启用 inline 模式
button 布尔值 true 显示右上角关闭按钮(jQuery 版本无效)
navbar 布尔值/整型 true 显示缩略图导航
title 布尔值/整型 true 显示当前图片的标题(现实 alt 属性及图片尺寸)
toolbar 布尔值/整型 true 显示工具栏
tooltip 布尔值 true 显示缩放百分比
movable 布尔值 true 图片是否可移动
zoomable 布尔值 true 图片是否可缩放
rotatable 布尔值 true 图片是否可旋转
scalable 布尔值 true 图片是否可翻转
transition 布尔值 true 使用 CSS3 过度
fullscreen 布尔值 true 播放时是否全屏
keyboard 布尔值 true 是否支持键盘
interval 整型 5000 播放间隔,单位为毫秒
zoomRatio 浮点型 0.1 鼠标滚动时的缩放比例
minZoomRatio 浮点型 0.01 最小缩放比例
maxZoomRatio 数字 100 最大缩放比例
zIndex 数字 2015 设置图片查看器 modal 模式时的 z-index
zIndexInline 数字 0 设置图片查看器 inline 模式时的 z-index
url 字符串/函数 src 设置大图片的 url
build 函数 null 回调函数,具体查看演示
built 函数 null 回调函数,具体查看演示
show 函数 null 回调函数,具体查看演示
shown 函数 null 回调函数,具体查看演示
hide 函数 null 回调函数,具体查看演示
hidden 函数 null 回调函数,具体查看演示
view 函数 null 回调函数,具体查看演示
viewed 函数 null 回调函数,具体查看演示