PhotoSwipe 图片浏览插件使用方法
一、简介
PhotoSwipe 是专为移动触摸设备设计的相册/画廊.兼容所有iPhone、iPad、黑莓6+,以及桌面浏览器.底层实现基于HTML/CSS/JavaScript,是一款免费开源的相册产品。

官方网站:http://photoswipe.com/
源码下载:https://github.com/dimsemenov/photoswipe
国内CDN:http://www.bootcdn.cn/photoswipe/
二、使用
1、新建html页面,如果是移动端看的话,需要在页面头部插入视口说明:
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
2、引入 photoswipe.css、default-skin.css 文件,引入 photoswipe.js、photoswipe-ui-default.js 文件。
<link href="http://cdn.bootcss.com/photoswipe/4.1.1/photoswipe.css" rel="stylesheet">
<link href="http://cdn.bootcss.com/photoswipe/4.1.1/default-skin/default-skin.css" rel="stylesheet">
<script src="http://cdn.bootcss.com/photoswipe/4.1.1/photoswipe.js"></script>
<script src="http://cdn.bootcss.com/photoswipe/4.1.1/photoswipe-ui-default.js"></script>
3、必要的HTML结构
<div class="repairImgList lightgallery" data-pswp-uid="1" id="PhotoSwipe-demo">
<figure>
<div>
<a href="http://192.168.0.99:9090/wyglFile/images/overhaul/03d1150a24a74e87ba89f689f1c374a7-20160328110004.jpg" data-size="640x1136"><img src="http://192.168.0.99:9090/wyglFile/images/overhaul/03d1150a24a74e87ba89f689f1c374a7-20160328110004.jpg"></a>
</div>
</figure>
<figure>
<div>
<a href="http://192.168.0.99:9090/wyglFile/images/overhaul/03d1150a24a74e87ba89f689f1c374a7-20160328110004.jpg" data-size="640x1136"><img src="http://192.168.0.99:9090/wyglFile/images/overhaul/03d1150a24a74e87ba89f689f1c374a7-20160328110004.jpg"></a>
</div>
</figure>
</div>
4、必要的启动代码
<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
<!-- Background of PhotoSwipe.
It's a separate element as animating opacity is faster than rgba(). -->
<div class="pswp__bg"></div>
<!-- Slides wrapper with overflow:hidden. -->
<div class="pswp__scroll-wrap">
<!-- Container that holds slides.
PhotoSwipe keeps only 3 of them in the DOM to save memory.
Don't modify these 3 pswp__item elements, data is added later on. -->
<div class="pswp__container">
<div class="pswp__item"></div>
<div class="pswp__item"></div>
<div class="pswp__item"></div>
</div>
<!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
<div class="pswp__ui pswp__ui--hidden">
<div class="pswp__top-bar">
<!-- Controls are self-explanatory. Order can be changed. -->
<div class="pswp__counter"></div>
<button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
<button class="pswp__button pswp__button--share" title="Share"></button>
<button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
<button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
<!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
<!-- element will get class pswp__preloader--active when preloader is running -->
<div class="pswp__preloader">
<div class="pswp__preloader__icn">
<div class="pswp__preloader__cut">
<div class="pswp__preloader__donut"></div>
</div>
</div>
</div>
</div>
<div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
<div class="pswp__share-tooltip"></div>
</div>
<button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
</button>
<button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
</button>
<div class="pswp__caption">
<div class="pswp__caption__center"></div>
</div>
</div>
</div>
</div>
这部分代码是相册组成的基本代码,由于PhotoSwipe没有集成到JS里面,所以需要手动的添加到body内,当然也可以采用JS方式把HTML代码拼接进去,我这里已经接好了,见下面JS:
var gelleryHtml = "";
gelleryHtml += "<div id='gallery' class='pswp' tabindex='-1' role='dialog' aria-hidden='true'>";
gelleryHtml += "<div class='pswp__bg'></div>";
gelleryHtml += "<div class='pswp__scroll-wrap'>";
gelleryHtml += "<div class='pswp__container'>";
gelleryHtml += "<div class='pswp__item'></div>";
gelleryHtml += "<div class='pswp__item'></div>";
gelleryHtml += "<div class='pswp__item'></div>";
gelleryHtml += "</div>";
gelleryHtml += "<div class='pswp__ui pswp__ui--hidden'>";
gelleryHtml += "<div class='pswp__top-bar'>";
gelleryHtml += "<div class='pswp__counter'></div>";
gelleryHtml += "<button class='pswp__button pswp__button--close' title='Close (Esc)'></button>";
gelleryHtml += "<button class='pswp__button pswp__button--share' title='Share' style='display:none'></button>";
gelleryHtml += "<button class='pswp__button pswp__button--fs' title='Toggle fullscreen'></button>";
gelleryHtml += "<button class='pswp__button pswp__button--zoom' title='Zoom in/out'></button>";
gelleryHtml += "<div class='pswp__preloader'>";
gelleryHtml += "<div class='pswp__preloader__icn'>";
gelleryHtml += "<div class='pswp__preloader__cut'>";
gelleryHtml += "<div class='pswp__preloader__donut'></div>";
gelleryHtml += "</div>";
gelleryHtml += "</div>";
gelleryHtml += "</div>";
gelleryHtml += "</div>";
gelleryHtml += "<div class='pswp__share-modal pswp__share-modal--hidden pswp__single-tap'>";
gelleryHtml += "<div class='pswp__share-tooltip'>";
gelleryHtml += "</div>";
gelleryHtml += "</div>";
gelleryHtml += "<button class='pswp__button pswp__button--arrow--left' title='Previous (arrow left)'></button>";
gelleryHtml += "<button class='pswp__button pswp__button--arrow--right' title='Next (arrow right)'></button>";
gelleryHtml += "<div class='pswp__caption'>";
gelleryHtml += "<div class='pswp__caption__center'>";
gelleryHtml += "</div>";
gelleryHtml += "</div>";
gelleryHtml += "</div>";
gelleryHtml += "</div>";
gelleryHtml += "</div>";
$(function() {
$('body').append(gelleryHtml);
})
5、最后,加上必要的启动代码:
var initPhotoSwipeFromDOM = function(gallerySelector) {
// 解析来自DOM元素幻灯片数据(URL,标题,大小...)
// (children of gallerySelector)
var parseThumbnailElements = function(el) {
var thumbElements = el.childNodes,
numNodes = thumbElements.length,
items = [],
figureEl,
linkEl,
size,
item,
divEl;
for (var i = 0; i < numNodes; i++) {
figureEl = thumbElements[i]; // <figure> element
// 仅包括元素节点
if (figureEl.nodeType !== 1) {
continue;
}
divEl = figureEl.children[0];
linkEl = divEl.children[0]; // <a> element
size = linkEl.getAttribute('data-size').split('x');
// 创建幻灯片对象
item = {
src: linkEl.getAttribute('href'),
w: parseInt(size[0], 10),
h: parseInt(size[1], 10)
};
if (figureEl.children.length > 1) {
// <figcaption> content
item.title = figureEl.children[1].innerHTML;
}
if (linkEl.children.length > 0) {
// <img> 缩略图节点, 检索缩略图网址
item.msrc = linkEl.children[0].getAttribute('src');
}
item.el = figureEl; // 保存链接元素 for getThumbBoundsFn
items.push(item);
}
return items;
};
// 查找最近的父节点
var closest = function closest(el, fn) {
return el && (fn(el) ? el : closest(el.parentNode, fn));
};
// 当用户点击缩略图触发
var onThumbnailsClick = function(e) {
e = e || window.event;
e.preventDefault ? e.preventDefault() : e.returnValue = false;
var eTarget = e.target || e.srcElement;
// find root element of slide
var clickedListItem = closest(eTarget, function(el) {
return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
});
if (!clickedListItem) {
return;
}
// find index of clicked item by looping through all child nodes
// alternatively, you may define index via data- attribute
var clickedGallery = clickedListItem.parentNode,
childNodes = clickedListItem.parentNode.childNodes,
numChildNodes = childNodes.length,
nodeIndex = 0,
index;
for (var i = 0; i < numChildNodes; i++) {
if (childNodes[i].nodeType !== 1) {
continue;
}
if (childNodes[i] === clickedListItem) {
index = nodeIndex;
break;
}
nodeIndex++;
}
if (index >= 0) {
// open PhotoSwipe if valid index found
openPhotoSwipe(index, clickedGallery);
}
return false;
};
// parse picture index and gallery index from URL (#&pid=1&gid=2)
var photoswipeParseHash = function() {
var hash = window.location.hash.substring(1),
params = {};
if (hash.length < 5) {
return params;
}
var vars = hash.split('&');
for (var i = 0; i < vars.length; i++) {
if (!vars[i]) {
continue;
}
var pair = vars[i].split('=');
if (pair.length < 2) {
continue;
}
params[pair[0]] = pair[1];
}
if (params.gid) {
params.gid = parseInt(params.gid, 10);
}
return params;
};
var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
var pswpElement = document.querySelectorAll('.pswp')[0],
gallery,
options,
items;
items = parseThumbnailElements(galleryElement);
// 这里可以定义参数
options = {
barsSize: {
top: 100,
bottom: 100
},
fullscreenEl: false,
shareButtons: [{
id: 'wechat',
label: '分享微信',
url: '#'
}, {
id: 'weibo',
label: '新浪微博',
url: '#'
}, {
id: 'download',
label: '保存图片',
url: '{{raw_image_url}}',
download: true
}],
// define gallery index (for URL)
galleryUID: galleryElement.getAttribute('data-pswp-uid'),
getThumbBoundsFn: function(index) {
// See Options -> getThumbBoundsFn section of documentation for more info
var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
rect = thumbnail.getBoundingClientRect();
return {
x: rect.left,
y: rect.top + pageYScroll,
w: rect.width
};
}
};
// PhotoSwipe opened from URL
if (fromURL) {
if (options.galleryPIDs) {
// parse real index when custom PIDs are used
for (var j = 0; j < items.length; j++) {
if (items[j].pid == index) {
options.index = j;
break;
}
}
} else {
// in URL indexes start from 1
options.index = parseInt(index, 10) - 1;
}
} else {
options.index = parseInt(index, 10);
}
// exit if index not found
if (isNaN(options.index)) {
return;
}
if (disableAnimation) {
options.showAnimationDuration = 0;
}
// Pass data to PhotoSwipe and initialize it
gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();
};
// loop through all gallery elements and bind events
var galleryElements = document.querySelectorAll(gallerySelector);
for (var i = 0, l = galleryElements.length; i < l; i++) {
galleryElements[i].setAttribute('data-pswp-uid', i + 1);
galleryElements[i].onclick = onThumbnailsClick;
}
// Parse URL and open gallery if it contains #&pid=3&gid=1
var hashData = photoswipeParseHash();
if (hashData.pid && hashData.gid) {
openPhotoSwipe(hashData.pid, galleryElements[hashData.gid - 1], true, true);
}
};
三、DEMO展示及下载
PhotoSwipe 图片浏览插件使用方法的更多相关文章
- PhotoSwipe图片展示插件
这个插件相当棒!功能也很强大,可以自行体会. 官方网址:http://www.photoswipe.com/ github地址:https://github.com/codecomputerlove/ ...
- photoswipe图片滑动插件使用
第一步: 引入jss和css文件 <!-- Core CSS file --> <link rel="stylesheet" href="path/t ...
- 不容错过的七个jQuery图片滑块插件
1.jQuery多图并列焦点图插件 今天我们要来分享一款比较特别的jQuery焦点图插件,它允许你自己定义当前画面的图片数量,在这个演示中,我们定义了3张图片一起显示.和其他jQuery焦点图一样,这 ...
- 前端不容错过的jQuery图片滑块插件
作为前端开发者,我们会碰到很到各种各样的jQuery插件,但老实说,很少有自己写的.今天要分享的几款jQuery图片滑块插件,也就是jQuery焦点图插件,基本上会在每个网站都有应用,可以下载看看,也 ...
- 前端开发不容错过的jQuery图片滑块插件(转)
作为前端开发者,我们会碰到很到各种各样的jQuery插件.今天要分享的几款jQuery图片滑块插件,也就是jQuery焦点图插件,基本上会在每个网站都有应用,可以下载看看,也许你可以用到. 1.jQu ...
- 使用iScroll和photoswipe写手机浏览图片的插件的几点经验
首先,当我知道我得到一个任务需要写一个在手机上能浏览图片的插件时,我第一想到了iScroll.它的左右滑动,上下滑动的效果在安卓手机上也能让用户有良好的体验,自己写也能方便控制. 我的需求是,插件要能 ...
- 分享22款响应式的 jQuery 图片滑块插件
响应式(Responsive)设计的目标是要让产品界面能够响应用户的行为,根据不同终端设备自动调整尺寸,带给用户良好的使用体验.这篇文章收集了22款优秀的响应式 jQuery 幻灯片插件,它们能够帮助 ...
- 移动端 H5图片裁剪插件,内置简单手势操作
前面曾经写过一篇<H5图片裁剪升级版>,但里面需要借助第三方手势库,这次就不需要使用手势库,全部封装在代码中. 下图是裁剪的展示,下面就做了拖放和裁剪,没有做缩放,在插件中需要用到大量的计 ...
- 图片缩放插件GestureImageView——Android 常用插件推荐(一)
Android 开发过程中,交互效果是一个非常繁琐的过程,甚至比Web开发过程中JS特效更加复杂.通过多年的发展,常用的交互方式已经发展相当成熟,而且有很多非常好的插件.为了避免重复造轮子,一些常用的 ...
随机推荐
- (转)elasticsearch5.2.2 压测配置
1.elasticsearch.yml # ---------------------------------- Cluster ----------------------------------- ...
- Windows搭建Log4Net+FileBeat+ELK日志分析系统过程
参考博客:http://udn.yyuap.com/thread-54591-1-1.html ; https://www.cnblogs.com/yanbinliu/p/6208626.html ; ...
- Spring mvc 数据验证框架注解
@AssertFalse 被注解的元素必须为false@AssertTrue 被注解的元素必须为false@DecimalMax(value) 被注解的元素必须为一个数字,其值必须小于等于指定的最小值 ...
- 解决Mac下npm权限问题
前言 在学习Vue-CLI3的时候使用了全局安装,提示安装失败,本以为是npm版本问题,在更新npm的过程中又出现了 npm ERR! code: 'EACCES' ,查了一下发现是权限问题. 看到权 ...
- dom对象转成jquery对象时候 变成数组 jquery转成dom时候 取数组第一个
- android面试(3)---基本问题
1.值类型,引用类型? 基本数据类型都是值类型:byte,short,int,long,float,double,char,boolean 其他类型都是引用类型. 引用类型在传入方法是,方法内部对引用 ...
- Linux进入单用户模式(passwd root修改密码)
进入单用户模式——passwd root修改密码 1.在grub 页面输入a,进入修改内核模式 2.在内核的结尾“/”,输入空格,在输入single,回车 3.启动系统,进入单用户模式 4.Passw ...
- Django 2.0 学习(15):Web框架
Web框架的本质 对于学习Python的同学,相信对Flask.Django.Web.py等不会陌生,这些都是Python语言的web框架.那么问题来了,web服务器是什么?它和web框架有什么关系? ...
- MySQL join 使用方法
JOIN 按照功能大致分为如下三类: INNER JOIN(内连接,或等值连接):取得两个表中存在连接匹配关系的记录. LEFT JOIN(左连接):取得左表(table1)完全记录,即是右表(tab ...
- (转)Java GC基本算法
http://blog.csdn.net/heyutao007/article/details/38151581 1.引用计数(reference counting) 原理:此对象有一个引用,则 ...