JavaScript 图像查看器
dist/
├── viewer.css
├── viewer.min.css (compressed)
├── viewer.js (UMD)
├── viewer.min.js (UMD, compressed)
├── viewer.common.js (CommonJS, default)
└── viewer.esm.js (ES Module)
npm install viewerjs
在浏览器中:
<link href="/path/to/viewer.css" rel="stylesheet">
<script src="/path/to/viewer.js"></script>
cdnjs 为 Viewer.js 的 CSS 和 JavaScript 提供 CDN 支持。您可以在此处 找到相关链接。
new Viewer(element[, options])
element
类型:HTMLElement
待查看的目标图片或图片容器。
options(可选)
类型:Object
待查看的选项。请查看可用的 options。
<!-- a block container is required -->
<div>
<img id="image" src="picture.jpg" alt="Picture">
</div>
<div>
<ul id="images">
<li><img src="picture-1.jpg" alt="Picture 1"></li>
<li><img src="picture-2.jpg" alt="Picture 2"></li>
<li><img src="picture-3.jpg" alt="Picture 3"></li>
</ul>
</div>
// You should import the CSS file.
// import 'viewerjs/dist/viewer.css';
import Viewer from 'viewerjs';
// View an image.
const viewer = new Viewer(document.getElementById('image'), {
inline: true,
viewed() {
viewer.zoomTo(1);
},
});
// Then, show the image by clicking it, or call `viewer.show()`.
// View a list of images.
// Note: All images within the container will be found by calling `element.querySelectorAll('img')`.
const gallery = new Viewer(document.getElementById('images'));
// Then, show one image by click it, or call `gallery.show()`.
仅在模态模式下可用。
Esc:退出全屏、关闭查看器、退出模态模式或停止播放。Space:停止播放。Tab:切换查看器中按钮的焦点状态。Enter:触发按钮上的点击事件处理程序。←:查看上一张图片。→:查看下一张图片。↑:放大图片。↓:缩小图片。Ctrl + 0:缩小至初始大小。Ctrl + 1:放大至正常大小。您可以使用 new Viewer(image, options) 设置查看器选项。 如果您想更改全局默认选项,可以使用 Viewer.setDefaults(options)。
Boolean or Stringtrue启用模态背景,为背景指定static,点击时不会关闭模态。
Booleantrue显示查看器右上角的按钮。
Boolean or Numbertrue0 或 false:隐藏导航栏1 或 true:显示导航栏2:仅当屏幕宽度大于 768 像素时显示导航栏3:仅当屏幕宽度大于 992 像素时显示导航栏4:仅当屏幕宽度大于 1200 像素时显示导航栏指定导航栏的可见性。
Boolean or Number or Function or Arraytrue0 或 false:隐藏标题1 或 true 或 Function 或 Array:显示标题2:仅在屏幕宽度大于 768 像素时显示标题3:仅在屏幕宽度大于 992 像素时显示标题4:仅在屏幕宽度大于 1200 像素时显示标题Function:自定义标题内容[Number, Function]:第一个元素指示可见性,第二个元素自定义标题内容指定标题的可见性和内容。
名称来自图片元素的
alt属性或从其 URL 解析出的图片名称。
For example, title: 4 equals to:
new Viewer(image, {
title: [4, (image, imageData) => `${image.alt} (${imageData.naturalWidth} × ${imageData.naturalHeight})`]
});
Boolean or Number or Objecttrue0 或 false:隐藏工具栏。1 或 true:显示工具栏。2:仅当屏幕宽度大于 768 像素时显示工具栏。3:仅当屏幕宽度大于 992 像素时显示工具栏。4:仅当屏幕宽度大于 1200 像素时显示工具栏。{ key: Boolean | Number }:显示或隐藏工具栏。{ key: String }:自定义按钮的大小。{ key: Function }:自定义按钮的点击处理程序。{ key: { show: Boolean | Number, size: String, click: Function }:自定义按钮的各个属性。指定工具栏及其按钮的可见性和布局。
For example, toolbar: 4 equals to:
new Viewer(image, {
toolbar: {
zoomIn: 4,
zoomOut: 4,
oneToOne: 4,
reset: 4,
prev: 4,
play: {
show: 4,
size: 'large',
},
next: 4,
rotateLeft: 4,
rotateRight: 4,
flipHorizontal: 4,
flipVertical: 4,
},
});
see more for custom toolbar.
String''添加到查看器根元素的自定义类名。
Element or String'body'用于将查看器置于模态模式的容器。
仅当“
inline”选项设置为“false”时可用。
Functionnull过滤图像以供查看(如果图像可查看则返回“true”,如果忽略图像则返回“false”)。
For example:
new Viewer(image, {
filter(image) {
return image.complete;
},
});
请注意,未设置
src属性的图像将被默认忽略。
Boolean or FullscreenOptionstrue启用播放时请求全屏。
需要浏览器支持 全屏 API。
Array['crossOrigin', 'decoding', 'isMap', 'loading', 'referrerPolicy', 'sizes', 'srcset', 'useMap']定义要从原始图像继承的额外属性。
请注意,基本属性
src和alt将始终从原始图像继承。
Number0.9定义查看图像的初始覆盖率。它必须是介于 0(0%)和 1(100%)之间的正数。
Number0定义要查看的图像的初始索引。
也用作
view方法的默认参数值。
Booleanfalse启用内联模式。
Number5000播放时自动循环图像之间的延迟时间。
Booleantrue启用键盘支持。
Booleantrue初始化时将焦点放在导航栏中的活动项目上。
需要将“
keyboard”选项设置为“true”。
Booleantrue指示加载图像时是否显示加载旋转器。
Booleantrue指示是否启用循环查看。
如果当前图像是最后一个,则下一个要查看的图像是第一个,反之亦然。
Number定义查看器的最小宽度。
仅在内联模式下可用(将“
inline”选项设置为“true”)。
Number定义查看器的最小高度。
仅在内联模式下可用(将“
inline”选项设置为“true”)。
Booleantrue启用移动图像。
Booleantrue启用旋转图像。
Booleantrue启用缩放图像。
Booleantrue启用以缩放图像。
Booleantrue通过在触摸屏上拖动可以缩放当前图像。
Booleantrue可以通过滚动鼠标来缩放图像。
Booleantrue通过在触摸屏上滑动即可滑动到下一张或上一张图像。
Booleantrue指示双击图片时是否在图片的自然大小和初始大小之间切换。
换句话说,双击图片时是否自动调用 toggle 方法。
需要
dblclick事件支持。
Booleantrue放大或缩小时显示具有图像比例(百分比)的工具提示。
Booleantrue对一些特殊元素启用 CSS3 过渡。
Number2015定义模态模式下查看器的 CSS z-index 值。
Number0以内联模式定义查看器的 CSS z-index 值。
Number0.1通过滚动鼠标定义缩放图像时的比例。
Number0.01定义缩小时图像的最小比例。
Number100定义图像放大时的最大比例。
String or Function'src'定义从何处获取原始图片 URL 以供查看。
如果是字符串,则应为每个图片元素的属性之一。
如果是函数,则应返回有效的图片 URL。
For example:
<img src="picture.jpg?size=160">
new Viewer(image, {
url(image) {
return image.src.replace('?size=160', '');
},
});
Functionnullready 事件的快捷方式。
Functionnullshow 事件的快捷方式。
Functionnullshown 事件的快捷方式。
Functionnullhide 事件的快捷方式。
Functionnullhidden 事件的快捷方式。
Functionnullview 事件的快捷方式。
FunctionnullShortcut of the viewed event.
FunctionnullShortcut of the move event.
FunctionnullShortcut of the moved event.
FunctionnullShortcut of the rotate event.
FunctionnullShortcut of the rotated event.
FunctionnullShortcut of the scale event.
FunctionnullShortcut of the scaled event.
FunctionnullShortcut of the zoom event.
FunctionnullShortcut of the zoomed event.
FunctionnullShortcut of the play event.
FunctionnullShortcut of the stop event.
所有方法都允许链式组合。
由于启动查看器时存在一些异步过程,因此您应该仅在方法可用时调用该方法,请参阅以下生命周期:
new Viewer(image, {
ready() {
// 2 methods are available here: "show" and "destroy".
},
shown() {
// 9 methods are available here: "hide", "view", "prev", "next", "play", "stop", "full", "exit" and "destroy".
},
viewed() {
// All methods are available here except "show".
this.viewer.zoomTo(1).rotateTo(180);
}
});
BooleanfalseShow the viewer.
Only available in modal mode.
BooleanfalseHide the viewer.
Only available in modal mode.
Number0 (inherits from the initialViewIndex option)查看带有图像索引的其中一张图片。如果查看器被隐藏,则会首先显示该图片。
viewer.view(1); // View the second image
BooleanfalseView the previous image.
BooleanfalseView the next image.
x:
Numbery (optional):
NumberxMove the image with relative offsets.
viewer.move(1);
viewer.move(-1, 0); // Move left
viewer.move(1, 0); // Move right
viewer.move(0, -1); // Move up
viewer.move(0, 1); // Move down
x:
Numbery (optional):
Numberx.Move the image to an absolute point.
NumberRotate the image with a relative degree.
viewer.rotate(90);
viewer.rotate(-90);
NumberRotate the image to an absolute degree.
viewer.rotateTo(0); // Reset to zero degree
viewer.rotateTo(360); // Rotate a full round
scaleX:
Number11 it does nothing.scaleY (optional):
NumberscaleX.Scale the image.
viewer.scale(-1); // Flip both horizontal and vertical
viewer.scale(-1, 1); // Flip horizontal
viewer.scale(1, -1); // Flip vertical
Number11 it does nothingScale the abscissa of the image.
viewer.scaleX(-1); // Flip horizontal
Number11 it does nothingScale the ordinate of the image.
viewer.scaleY(-1); // Flip vertical
ratio:
NumbershowTooltip (optional):
Booleanfalsepivot (optional):
Objectnull{ x: Number, y: Number }Zoom the image with a relative ratio
viewer.zoom(0.1);
viewer.zoom(-0.1);
ratio:
NumbershowTooltip (optional):
Booleanfalsepivot (optional):
Objectnull{ x: Number, y: Number }Zoom the image to an absolute ratio.
viewer.zoomTo(0); // Zoom to zero size (0%)
viewer.zoomTo(1); // Zoom to natural size (100%)
// Zoom to 50% from the center of the window.
viewer.zoomTo(.5, {
x: window.innerWidth / 2,
y: viewer.innerHeight / 2,
});
Boolean or FullscreenOptionsfalsePlay the images.
Stop play.
Enter the modal mode.
Only available in inline mode.
Exit the modal mode.
Only available in inline mode.
Show the current ratio of the image by percentage.
Requires the
tooltipoption set totrue.
Toggle the image size between its current size and natural size.
Used by the
toggleOnDblclickoption.
Reset the image to its initial state.
当源图像发生更改(添加、删除或排序)时,更新查看器实例。
如果您动态加载图像(使用 XMLHTTPRequest),则可以使用此方法将新图像添加到查看器实例。
Destroy the viewer and remove the instance.
所有事件都可以在其处理程序中使用 this.viewer 来访问查看器实例。
请谨慎将这些事件与其他具有相同事件名称的组件一起使用,例如:Bootstrap 的模态框。
let viewer;
image.addEventListener('viewed', function () {
console.log(this.viewer === viewer);
// > true
});
viewer = new Viewer(image);
truetruenull当查看器实例准备好查看时,此事件会触发。
在模态模式下,只有点击其中一张图片时才会触发此事件。
truetruenullThis event fires when the viewer modal starts to show.
Only available in modal mode.
truetruenullThis event fires when the viewer modal has shown.
Only available in modal mode.
truetruenullThis event fires when the viewer modal starts to hide.
Only available in modal mode.
truefalsenullThis event fires when the viewer modal has hidden.
Only available in modal mode.
truetrueNumberHTMLImageElementHTMLImageElementThis event fires when a viewer starts to show (view) an image.
truefalseview event.This event fires when a viewer has shown (viewed) an image.
truetrueNumberNumberNumberNumberEvent or nullpointermove, touchmove, and mousemove.This event fires when a viewer starts to move an image.
truefalsemove event.This event fires when a viewer has moved an image.
truetrueNumberNumberThis event fires when a viewer starts to rotate an image.
truefalserotate event.This event fires when a viewer has rotated an image.
truetrueNumberNumberNumberNumberThis event fires when a viewer starts to scale an image.
truefalsescale event.This event fires when a viewer has scaled an image.
truetrueNumberimageData.width / imageData.naturalWidth).NumberEvent or nullwheel, pointermove, touchmove, and mousemove.This event fires when a viewer starts to zoom (in or out) an image.
truefalsezoom event.This event fires when a viewer has zoomed (in or out) an image.
truetruenullThis event fires when the viewer starts to play.
You can abort the playing process by calling
event.preventDefault().
truetruenullThis event fires when the viewer starts to stop.
You can abort the stopping process by calling
event.preventDefault().
如果您必须使用具有相同命名空间的另一个查看器,请调用“Viewer.noConflict”静态方法来恢复它。
<script src="other-viewer.js"></script>
<script src="viewer.js"></script>
<script>
Viewer.noConflict();
// Code that uses other `Viewer` can follow here.
</script>
Please read through our contributing guidelines.
Maintained under the Semantic Versioning guidelines.