网站没有图片放大预览功能,当自己在文章中插入大尺寸图片时,在网页看图片细节会看不清,这种情况要不右键保存图片到本地,要不复制图片链接在新窗口打开才能看到图片中细节。其实只需要小小的js插件即可实现图片预览效果。
今天介绍的插件Viewer.js,效果可尝试下图。点击图片预览,鼠标滚轮或键盘方向键上下可放大缩小,左右方向键可切换图片,ESC或点击X键退出预览,并且支持幻灯片播放。
实现方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="css/viewer.min.css">
<script src="js/viewer.min.js"></script>
</head>
<body>
<img src="./img/tibet-1.jpg" alt="图片1">
<img src="./img/tibet-2.jpg" alt="图片2">
</body>
<script>
// 这里选创建浏览对象所在范围的元素,body全通用:
// 本页采用的是document.getElementsByClassName("article-content")[0])
// 即根据文章内容class获取文章内容元素 然对其进行创建预览对象
var viewer = new Viewer(document.body);
</script>
</html>
其实也就是简单调用其js与css,然后利用new Viewer()调用即可。JQ版本调用为:
$('#id').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 | 回调函数,具体查看演示 |
配置也很简单,把参数配置写在new Viewer第二个参数中即可:
new Viewer(document.body, {
url: 'data-original',
button: true,
navbar: true
});
项目示例:
//父页面函数
/**
* 初始化按钮,利用datagrid的row来自动生成每行对一个的图片的一个预览按钮
* 因为img元素在datagrid的formatter里面,好像是不能触发viewer.js的img的click事件,
* 这里采用每一个预览按钮在父页面生成一个imp的隐藏按钮,预览按钮的click事件绑定了这个img的click事件
* @param rows
* @returns
*/
function initImageView(rows){
$('.image').remove();
for(var i = 0;i < rows.length;i++){
var row = rows[i];
if(row.attachmentName && (row.attachmentName.indexOf("jpg") > -1 || row.attachmentName.indexOf("png") > -1 ) ){
var appendStr = '<img src="'+row.downUri+'" title="图片预览" alt="图片预览" style="width: 20px; height: 15px;margin-top: 2px;display: none" id="image_'+row.id+'" class="image"/>';
$(appendStr).appendTo($('body'));
}
}
var viewer = new Viewer(document.body);
}
/**
* 点击预览图片
* @param row
* @returns
*/
function showImageView(row){
var $rowImage = $('#image_'+row.id);
if($rowImage.length == 0){
var appendStr = '<img id="myImage" src="'+row.downUri+'" title="图片预览" alt="图片预览" style="width: 20px; height: 15px;margin-top: 2px;display: none" id="image_'+row.id+'" class="image"/>';
$(appendStr).appendTo($('body'));
$rowImage = $('#image_'+row.id);
}
$rowImage.trigger('click');//绑定了click事件
}
//子页面
$({
$("#xgzlDataGrid").datagrid({
title: '',
fit:true,
method: "get",
loadMsg:'正在加载附件列表,请稍待...',
singleSelect:false,
url: './searchData',
idField: 'id',
rownumbers:true,
fitColumns:false,
extEditing: true,
frozenColumns: [[
]],
columns: [[
{ field: 'id', title: '', width: 50,editor:{type:'text'},halign:'center',align:'center',hidden:true},
{ field: 'attachmentName', title: '附件名称', width: 220,tooltip:true,halign:'center',align:'lift'},
{ field: 'elementAttachment', title: '', width: 50,halign:'center',align:'center',hidden:true},
{ field: 'attachmentRequired', title: '操作', width: 180,halign:'center',align:'center',
formatter:function(index,row){
var optName = "上传";
var str = "";
if(row.uri){
if(row.attachmentName.indexOf("jpg")>-1||row.attachmentName.indexOf("png")>-1){
str += "|<a class=\"l-btn l-btn-small l-btn-plain \" onclick=\"selectAttachment(this)\" title=\"附件预览\" data-options=\"plain: true,title:'附件预览'\"><span class=\"l-btn-left l-btn-icon-left\"><span class=\"l-btn-text\">预览</span><span class=\"l-btn-icon icon-hamburg-graphy\"></span></span></a>";
}
}
return str;
}
}
]],topDrag:function(row){
},
onLoadSuccess:function(data)
{
window.parent.initImageView(data.rows);
},
});
})
/**
* 图片预览
* @param param
*/
function selectAttachment(param){
var index = $(param).parents("tr").attr('datagrid-row-index');
var row = $('#xgzlDataGrid').datagrid('getRowData',parseInt(index));
window.parent.showImageView(row);
return false;
}