# 图片预览
安装或者引入viewerjs
# viewerjs
- 支持移动设备触摸事件
- 支持响应式
- 支持放大/缩小
- 支持旋转(类似微博的图片旋转)
- 支持水平/垂直翻转
- 支持图片移动
- 支持键盘
- 支持全屏幻灯片模式(可做屏保)
- 支持缩略图
- 支持标题显示
- 支持多种自定义事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="./viewer.css"/>
<script src="./viewer.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
ul,li{
list-style: none;
}
.img-list{
clear: both;
}
.img-list li{
width:100px;
height: 100px;
margin: 10px;
float: left;
}
img{
width: 100px;
height: 100px;
display: block;
}
.left{
width:50px;
height:50px;
background:deepskyblue;
line-height: 50px;
position:fixed;
top:0;
left:0;
text-align:center;
color:#FFF;
border-radius: 100%;
cursor:pointer;
z-index:10000;
}
</style>
</head>
<body>
<ul id="images1" class="img-list">
<li>
<img src="apple.jpg"/>
</li>
<li>
<img src="07.png"/>
</li>
<li>
<img src="09.png" data-imgurl="09.png"/>
</li>
</ul>
<div class="left" onclick="ps()">←</div>
<div class="right"></div>
</body>
<script type="text/javascript">
var viewer = new Viewer(document.getElementById('images1'), {
inline:false,
navbar:false,
title:false,
transition:false,
});
function ps(){
viewer.prev(true);
}
</script>
</html>
# 配置
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
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 | 回调函数,具体查看演示 |
| inline | 布尔值 | false | 启用 inline 模式 |
| button | 布尔值 | true | 显示右上角关闭按钮(jQuery 版本无效) |
函数类似于vue react的生命周期
# 自定义方法
var viewer = new Viewer(document.getElementById('dowebok'), {
url: 'data-original'
});
document.getElementById('btn1').onclick = function() {
viewer.show();
}
document.getElementById('btn2').onclick = function() {
viewer.hide();
}
document.getElementById('btn3').onclick = function() {
viewer.view(5);
}
document.getElementById('btn4').onclick = function() {
viewer.next();
}
document.getElementById('btn5').onclick = function() {
viewer.prev();
}
document.getElementById('btn6').onclick = function() {
viewer.move(-10, 0);
}
document.getElementById('btn7').onclick = function() {
viewer.moveTo(0, 0);
}
document.getElementById('btn8').onclick = function() {
viewer.zoom(0.1);
}
document.getElementById('btn9').onclick = function() {
viewer.zoomTo(1);
}
document.getElementById('btn10').onclick = function() {
viewer.rotate(90);
}
document.getElementById('btn11').onclick = function() {
viewer.rotateTo(180);
}
document.getElementById('btn12').onclick = function() {
viewer.scale(-1);
}
document.getElementById('btn13').onclick = function() {
viewer.scaleX(-1);
}
document.getElementById('btn14').onclick = function() {
viewer.scaleY(-1);
}
document.getElementById('btn15').onclick = function() {
viewer.play();
}
document.getElementById('btn16').onclick = function() {
viewer.stop();
}
document.getElementById('btn17').onclick = function() {
viewer.full();
}
document.getElementById('btn18').onclick = function() {
viewer.exit();
}
document.getElementById('btn19').onclick = function() {
viewer.tooltip();
}
document.getElementById('btn20').onclick = function() {
viewer.toggle();
}
document.getElementById('btn21').onclick = function() {
viewer.reset();
}
document.getElementById('btn22').onclick = function() {
viewer.destroy();
}