一、简介

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 图片浏览插件使用方法的更多相关文章

  1. PhotoSwipe图片展示插件

    这个插件相当棒!功能也很强大,可以自行体会. 官方网址:http://www.photoswipe.com/ github地址:https://github.com/codecomputerlove/ ...

  2. photoswipe图片滑动插件使用

    第一步:  引入jss和css文件 <!-- Core CSS file --> <link rel="stylesheet" href="path/t ...

  3. 不容错过的七个jQuery图片滑块插件

    1.jQuery多图并列焦点图插件 今天我们要来分享一款比较特别的jQuery焦点图插件,它允许你自己定义当前画面的图片数量,在这个演示中,我们定义了3张图片一起显示.和其他jQuery焦点图一样,这 ...

  4. 前端不容错过的jQuery图片滑块插件

    作为前端开发者,我们会碰到很到各种各样的jQuery插件,但老实说,很少有自己写的.今天要分享的几款jQuery图片滑块插件,也就是jQuery焦点图插件,基本上会在每个网站都有应用,可以下载看看,也 ...

  5. 前端开发不容错过的jQuery图片滑块插件(转)

    作为前端开发者,我们会碰到很到各种各样的jQuery插件.今天要分享的几款jQuery图片滑块插件,也就是jQuery焦点图插件,基本上会在每个网站都有应用,可以下载看看,也许你可以用到. 1.jQu ...

  6. 使用iScroll和photoswipe写手机浏览图片的插件的几点经验

    首先,当我知道我得到一个任务需要写一个在手机上能浏览图片的插件时,我第一想到了iScroll.它的左右滑动,上下滑动的效果在安卓手机上也能让用户有良好的体验,自己写也能方便控制. 我的需求是,插件要能 ...

  7. 分享22款响应式的 jQuery 图片滑块插件

    响应式(Responsive)设计的目标是要让产品界面能够响应用户的行为,根据不同终端设备自动调整尺寸,带给用户良好的使用体验.这篇文章收集了22款优秀的响应式 jQuery 幻灯片插件,它们能够帮助 ...

  8. 移动端 H5图片裁剪插件,内置简单手势操作

    前面曾经写过一篇<H5图片裁剪升级版>,但里面需要借助第三方手势库,这次就不需要使用手势库,全部封装在代码中. 下图是裁剪的展示,下面就做了拖放和裁剪,没有做缩放,在插件中需要用到大量的计 ...

  9. 图片缩放插件GestureImageView——Android 常用插件推荐(一)

    Android 开发过程中,交互效果是一个非常繁琐的过程,甚至比Web开发过程中JS特效更加复杂.通过多年的发展,常用的交互方式已经发展相当成熟,而且有很多非常好的插件.为了避免重复造轮子,一些常用的 ...

随机推荐

  1. 0302IT行业虽吃香,能完全享受这块“香"的也很难

    面对现今严峻的就业形势,越来越多的人希望通过职业技能培训或者学历提升来提高自己的综合技能以便能够顺利地应聘到自己理想中的工作. 在2014年十大最热门行业和职业排行榜中IT行业最吃香.在十大行业里,I ...

  2. 找"数学口袋精灵"bug

    团队成员的博客园地址: 刘森松:http://home.cnblogs.com/u/lssh/ 郭志豪:http://home.cnblogs.com/u/gzh13692021053/ 谭宇森:ht ...

  3. PHP用抛物线的模型实现微信红包生成算法的程序源码

    <?php /* *Author:Kermit *Time:2015-8-26 *Note:红包生成随机算法 */ header("Content-type:text/html;cha ...

  4. vue+postcss报错: variable '--primary-color' is undefined and used without a fallback

    之前vue-cli3引入postcss的配置: https://www.cnblogs.com/XHappyness/p/7676680.html 发现这么一个问题,我再全局global.css中定义 ...

  5. influxdb 命令

    写入数据: curl -X POST -d '[{"name":"foo","columns":["val"],&quo ...

  6. 外部JS的阻塞下载

    转载于:http://www.cnblogs.com/mofish/archive/2011/09/29/2195256.html 所有浏览器在下载JS的时候,会阻止一切其他活动,比如其他资源的下载, ...

  7. 第192天:js---Date对象属性和方法总结

    Date对象构造函数重载方法 一.第一种重载方法---基本 当前时间 //构造函数 - 第一种重载方法:基本 当前时间 console.log('构造函数 - 第一种重载方法:基本 当前时间') da ...

  8. UVA12545_Bits Equalizer

    题目意思很简单,给你两个串,第一个串为0,1或者?,第二个串为0,1, 每次你可以对第一个串进行三种操作,1.0变为1:2.?变为0或者1:3.交换任意两个数的位置. 现在问你能否把第一个串变为第一个 ...

  9. hadoop和spark搭建记录

    因玩票需要,使用三台搭建spark(192.168.1.10,192.168.1.11,192.168.1.12),又因spark构建在hadoop之上,那么就需要先搭建hadoop.历经一个两个下午 ...

  10. 2017 ACM Arabella Collegiate Programming Contest(solved 9/13, complex 12/13)

    A.Sherlock Bones 题意: 给出长度为n的01串,问f(i,j)=f(j,k),(i<j<k)的i,j,k取值种数.其中f(i,j)表示[i,j]内1的个数, 且s[j]必须 ...