入门

您应知道之前先做起事情:

1. PhotoSwipe不是一个简单的jQuery插件,至少基本的JavaScript知识才能安装。

2. PhotoSwipe需要预定义的图像尺寸(更多关于这一点)。

3. 如果您在非回应网站上使用PhotoSwipe - 控制将在移动进行换算(整页缩放)。所以你需要实现自定义控件(在右上角例如单个大关闭按钮)。

4. 文档中所有的代码是纯香草JS和支持IE8及以上。如果您的网站或应用程序使用了一些JavaScript框架(像jQuery或MooTools的),或者你并不需要支持旧的浏览器 - 随意简化代码。

5. 避免对移动服务的大图像(大于2000x1500px),因为它们会极大地降低动画性能,并可能导致崩溃(尤其是iOS上的Safari浏览器)。可能的解决方案:一个单独的网页上投放响应图像,或者打开的图像,或者(在常见问题解答更多)使用支持平铺图像(如传单)库。

初始化

第1步:包括JS和CSS文件

您可以在GitHub的信息库DIST/文件夹中找到它们。萨斯和未编译的JS文件夹中的src /。我建议使用无礼的话,如果你打算修改现有的样式,有代码的结构和评述。https://github.com/dimsemenov/PhotoSwipe

 <!-- Core CSS file -->
<link rel="stylesheet" href="path/to/photoswipe.css"> <!-- Skin CSS file (styling of UI - buttons, caption, etc.)
In the folder of skin CSS file there are also:
- .png and .svg icons sprite,
- preloader.gif (for browsers that do not support CSS animations) -->
<link rel="stylesheet" href="path/to/default-skin/default-skin.css"> <!-- Core JS file -->
<script src="path/to/photoswipe.min.js"></script> <!-- UI JS file -->
<script src="path/to/photoswipe-ui-default.min.js"></script>

不要紧,如何以及在哪里将包括JS和CSS文件。只有当你调用新PhotoSwipe代码被执行()。可以随意推迟文件加载,如果你不需要PhotoSwipe被初步打开。

PhotoSwipe还支持AMD装载机(如RequireJS)和CommonJS的,使用起来就像这样:

 require([
'path/to/photoswipe.js',
'path/to/photoswipe-ui-default.js'
], function( PhotoSwipe, PhotoSwipeUI_Default ) { // var gallery = new PhotoSwipe( someElement, PhotoSwipeUI_Default ...
// gallery.init()
// ... });

而且,您可以通过鲍尔安装(安装鲍尔photoswipe),或NPM(NPM安装photoswipe)。

第2步:PhotoSwipe(.pswp)元素添加到DOM

您可以通过JS动态添加HTML代码(直接在初始化之前),或者有它的页面开始(喜欢它的演示页面上完成)。该代码可以在任何地方附加,但理想的结束</ body>之前。 (你用相同的UI类只要)你可以重复使用它在多个画廊。

 <!-- 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>

pswp__bg,pswp__scroll换行,pswp__container和pswp__item元素的顺序不应改变。

你可能会问,为什么PhotoSwipe不通过JS自动添加该代码,原因很简单 - 只是为了节省文件大小的情况下,如果你需要的布局做一些修改。

步骤3:初始化

执行PhotoSwipe构造函数。它接受4个参数:

1. 从步骤2(它必须被添加到DOM).pswp元件。

2. PhotoSwipe UI类。如果包括了默认photoswipe-UI-default.js,类将是PhotoSwipeUI_Default。可以是假的。

3. 与对象(幻灯片)阵列。

4. options

 var pswpElement = document.querySelectorAll('.pswp')[0];

 // build items array
var items = [
{
src: 'https://placekitten.com/600/400',
w: 600,
h: 400
},
{
src: 'https://placekitten.com/1200/900',
w: 1200,
h: 900
}
]; // define options (if needed)
var options = {
// optionName: 'option value'
// for example:
index: 0 // start at first slide
}; // Initializes and opens PhotoSwipe
var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();

在最后你应该得到的东西是这样的:

html:

 <button id="btn">Open PhotoSwipe</button>

 <!-- 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 slides in DOM to save memory. -->
<div class="pswp__container">
<!-- don't modify these 3 pswp__item elements, data is added later on -->
<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>

js :

var openPhotoSwipe = function() {
var pswpElement = document.querySelectorAll('.pswp')[0]; // build items array
var items = [
{
src: 'https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_b.jpg',
w: 964,
h: 1024
},
{
src: 'https://farm7.staticflickr.com/6175/6176698785_7dee72237e_b.jpg',
w: 1024,
h: 683
}
]; // define options (if needed)
var options = {
// history & focus options are disabled on CodePen
history: false,
focus: false, showAnimationDuration: 0,
hideAnimationDuration: 0 }; var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();
}; openPhotoSwipe(); document.getElementById('btn').onclick = openPhotoSwipe;

结果:

创建幻灯片对象的数组

数组应包含有关幻灯片数据中的每个对象,也可以是你希望在PhotoSwipe显示任何东西 - 路径图像,标题字符串,股数,评论等。

默认情况下PhotoSwipe只使用了5个属性:SRC(路径图),W(图像宽度),H(图像高度),MSRC(路径的小图像占位符,大的图像会在上面装),HTML(自定义HTML,更 关于它)。

在导航,PhotoSwipe增加自己的属性到这个对象(如MINZOOM或加载)。

 var slides = [

     // slide 1
{ src: 'path/to/image1.jpg', // path to image
w: 1024, // image width
h: 768, // image height msrc: 'path/to/small-image.jpg', // small image placeholder,
// main (large) image loads on top of it,
// if you skip this parameter - grey rectangle will be displayed,
// try to define this property only when small image was loaded before title: 'Image Caption' // used by Default PhotoSwipe UI
// if you skip it, there won't be any caption // You may add more properties here and use them.
// For example, demo gallery uses "author" property, which is used in the caption.
// author: 'John Doe' }, // slide 2
{
src: 'path/to/image2.jpg',
w: 600,
h: 600 // etc.
} // etc. ];

您可以动态地定义幻灯片对象的属性PhotoSwipe读取它们直接之前,使用gettingData事件(在文档的API部分的更多信息)。例如,该技术可用于为不同的屏幕尺寸不同的图像。

如何从一个链接列表建立幻灯片的数组

让我们假设你有一个看起来像这样(约画廊的标记更多信息)的链接/缩略图列表:

 <div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery">

     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="large-image.jpg" itemprop="contentUrl" data-size="600x400">
<img src="small-image.jpg" itemprop="thumbnail" alt="Image description" />
</a>
<figcaption itemprop="caption description">Image caption</figcaption>
</figure> <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="large-image.jpg" itemprop="contentUrl" data-size="600x400">
<img src="small-image.jpg" itemprop="thumbnail" alt="Image description" />
</a>
<figcaption itemprop="caption description">Image caption</figcaption>
</figure> </div>

...你要点击缩略图与大型图像打开PhotoSwipe(喜欢它的演示页上完成)。所有你需要做的是:

1. 绑定click事件链接/缩略图。

2. 用户点击缩略图后,找到它的索引。

3. 创建DOM元素幻灯片对象的数组 - 通过各环节循环和检索href属性(大图像URL),数据大小属性(其大小),缩略图的SRC和字幕的内容。

PhotoSwipe并不真正关心你将如何做到这一点。如果你使用像jQuery或MooTools的框架,或者如果你不需要支持IE8,代码可以大大简化。

这里是纯香草JS实现与IE8的支持:

 var initPhotoSwipeFromDOM = function(gallerySelector) {

     // parse slide data (url, title, size ...) from DOM elements
// (children of gallerySelector)
var parseThumbnailElements = function(el) {
var thumbElements = el.childNodes,
numNodes = thumbElements.length,
items = [],
figureEl,
linkEl,
size,
item; for(var i = 0; i < numNodes; i++) { figureEl = thumbElements[i]; // <figure> element // include only element nodes
if(figureEl.nodeType !== 1) {
continue;
} linkEl = figureEl.children[0]; // <a> element size = linkEl.getAttribute('data-size').split('x'); // create slide object
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> thumbnail element, retrieving thumbnail url
item.msrc = linkEl.children[0].getAttribute('src');
} item.el = figureEl; // save link to element for getThumbBoundsFn
items.push(item);
} return items;
}; // find nearest parent element
var closest = function closest(el, fn) {
return el && ( fn(el) ? el : closest(el.parentNode, fn) );
}; // triggers when user clicks on thumbnail
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); // define options (if needed)
options = { // 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
// http://photoswipe.com/documentation/faq.html#custom-pid-in-url
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 );
}
}; // execute above function
initPhotoSwipeFromDOM('.my-gallery');

例如在CodePen(重点和历史选项被禁用,由于嵌入的问题):

 <h2>First gallery:</h2>

   <div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery">

     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm3.staticflickr.com/2567/5697107145_a4c2eaa0cd_o.jpg" itemprop="contentUrl" data-size="1024x1024">
<img src="https://farm3.staticflickr.com/2567/5697107145_3c27ff3cd1_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
<figcaption itemprop="caption description">Image caption 1</figcaption> </figure> <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_b.jpg" itemprop="contentUrl" data-size="964x1024">
<img src="https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
<figcaption itemprop="caption description">Image caption 2</figcaption>
</figure> <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm7.staticflickr.com/6175/6176698785_7dee72237e_b.jpg" itemprop="contentUrl" data-size="1024x683">
<img src="https://farm7.staticflickr.com/6175/6176698785_7dee72237e_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
<figcaption itemprop="caption description">Image caption 3</figcaption>
</figure> <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm6.staticflickr.com/5023/5578283926_822e5e5791_b.jpg" itemprop="contentUrl" data-size="1024x768">
<img src="https://farm6.staticflickr.com/5023/5578283926_822e5e5791_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
<figcaption itemprop="caption description">Image caption 4</figcaption>
</figure> </div> <h2>Second gallery:</h2> <div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery"> <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_b.jpg" itemprop="contentUrl" data-size="964x1024">
<img src="https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
<figcaption itemprop="caption description">Image caption 2.1</figcaption>
</figure> <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm7.staticflickr.com/6175/6176698785_7dee72237e_b.jpg" itemprop="contentUrl" data-size="1024x683">
<img src="https://farm7.staticflickr.com/6175/6176698785_7dee72237e_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
<figcaption itemprop="caption description">Image caption 2.2</figcaption>
</figure> <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm6.staticflickr.com/5023/5578283926_822e5e5791_b.jpg" itemprop="contentUrl" data-size="1024x768">
<img src="https://farm6.staticflickr.com/5023/5578283926_822e5e5791_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
<figcaption itemprop="caption description">Image caption 2.3</figcaption>
</figure> </div> <!-- 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 slides in 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>
 .my-gallery {
width: 100%;
float: left;
}
.my-gallery img {
width: 100%;
height: auto;
}
.my-gallery figure {
display: block;
float: left;
margin: 0 5px 5px 0;
width: 150px;
}
.my-gallery figcaption {
display: none;
}
 var initPhotoSwipeFromDOM = function(gallerySelector) {

     // parse slide data (url, title, size ...) from DOM elements
// (children of gallerySelector)
var parseThumbnailElements = function(el) {
var thumbElements = el.childNodes,
numNodes = thumbElements.length,
items = [],
figureEl,
linkEl,
size,
item; for(var i = 0; i < numNodes; i++) { figureEl = thumbElements[i]; // <figure> element // include only element nodes
if(figureEl.nodeType !== 1) {
continue;
} linkEl = figureEl.children[0]; // <a> element size = linkEl.getAttribute('data-size').split('x'); // create slide object
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> thumbnail element, retrieving thumbnail url
item.msrc = linkEl.children[0].getAttribute('src');
} item.el = figureEl; // save link to element for getThumbBoundsFn
items.push(item);
} return items;
}; // find nearest parent element
var closest = function closest(el, fn) {
return el && ( fn(el) ? el : closest(el.parentNode, fn) );
}; // triggers when user clicks on thumbnail
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); // define options (if needed)
options = { // 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
// http://photoswipe.com/documentation/faq.html#custom-pid-in-url
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 );
}
}; // execute above function
initPhotoSwipeFromDOM('.my-gallery');

提示:您可以从CodePen例如下载在本地发挥它(编辑上CodePen - >分享 - >导出.zip文件)。

如果您使用的标记,从这个例子不同,你需要编辑功能解析缩略图元素。

如果你没有在纯JavaScript经验,不知道如何解析DOM,请参阅怪异模式和文档在MSDN上。

需要注意的是IE8不支持HTML5<图>和<figcaption>元素,所以你需要在<head>部分(托管版本的例子中使用cdnjs)html5shiv:

<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js"></script>
<![endif]-->

PhotoSwipe中文API(一)的更多相关文章

  1. PhotoSwipe中文API(三)

    http://photoswipe.com/documentation/api.html 所有的方法和这个网页上列出的属性是公开的.如果你想看看例子什么API可以做的,拿在默认PhotoSwipe U ...

  2. PhotoSwipe中文API(二)

    配置 选项是在键 - 值对添加作为参数传递给PhotoSwipe构造,例如通过: var options = { index: 3, escKey: false, // ui option timeT ...

  3. PhotoSwipe中文API(五)

    Responsive Images PhotoSwipe不支持<图片>或srcset,因为它要求所定义的图像的尺寸,并使用延迟加载.但是,随着图像动态加载,它很容易切换人士透露,即便是在旧 ...

  4. PhotoSwipe中文API(四)

    在幻灯片自定义HTML内容 为了使PhotoSwipe显示HTML内容的幻灯片,你需要在幻灯片对象定义html属性.它应该包含HTML字符串或DOM元素对象. var items = [ // sli ...

  5. [转载]fullPage.js中文api 配置参数~

    fullPage.js中文api 配置参数 选项 类型 默认值 说明 verticalCentered 字符串 true 内容是否垂直居中 resize 布尔值 false 字体是否随着窗口缩放而缩放 ...

  6. android SDK下载及中文API地址

    中文API:http://wiki.eoeandroid.com/Android_API_Guides Android Dev Tools官网地址:www.androiddevtools.cn 收集整 ...

  7. JQUERY MOBILE 中文API站 和 官方论坛

    中文API站:http://www.jqmapi.com/api1.2/preview/quickstartquide.html 官方论坛:http://bbs.phonegapcn.com/foru ...

  8. 设置Eclipse中文API提示信息

    准备工作:下载中文API到本机:http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/html_ ...

  9. [置顶] COcos2d-X 中文API

    本文来自http://blog.csdn.net/runaying ,引用必须注明出处! COcos2d-X 中文API 温馨提醒:使用二维码扫描软件,就可以在手机上访问我的博客啦!另外大家可以访问另 ...

随机推荐

  1. ASP.NET动态添加用户控件的方法

    本文实例讲述了ASP.NET动态添加用户控件的方法.分享给大家供大家参考.具体实现方法如下: 为了让用户控件能ASP.NET页面实现动态添加,首先写一个接口IGetUCable,这个接口有一个函数,返 ...

  2. ChemDraw常用到的几种技巧

    ChemDraw对于化学学习的重要性相当于CAD和建筑学.PS和设计,所以如果你是一名生化相关的工作人员,拥有ChemDraw并掌握ChemDraw的使用方法十分必要!这是一款对于我们研究化学的人来说 ...

  3. Linux 内核中 likely 与 unlikely 的宏定义解析

    在 2.6 内核中,随处能够见到 likely() 和 unlikely() 的身影,那么为什么要用它们?它们之间有什么差别? 首先要明白: if(likely(value)) 等价于 if(valu ...

  4. web服务器http.server 【python】

    参考博客: http://lxneng.iteye.com/blog/492063 http://www.cnblogs.com/itech/archive/2011/12/31/2308697.ht ...

  5. js里面setInterval和setTimeout相同点和区别

    相同点:两个方法都是先触发间隔时间,再触发回调函数 区别: 1.setInterval每隔指定的时间就执行一次表达式,若不停止会一直执行下去 而setTimeout在执行时,是在载入后延迟指定时间后, ...

  6. openstack将本地实例迁移至ceph存储中

    需求: 最近在openstack上线了ceph存储,创建虚拟机和云硬盘都基于ceph卷进行存储和创建,但是之前openstack用的是本地存储,创建的所有实例都在本地文件中,当实例重启之后,opens ...

  7. poj_1988 并查集

    题目大意 开始有N堆砖块,编号为1,2....N,每堆都只有一个.之后可以进行两种操作: (1)M X Y 将编号为X的砖块所在的那堆砖拿起来放到编号为Y的砖块所在的堆上: (2)C X 查询编号为X ...

  8. luogu P2066 机器分配[背包dp+方案输出]

    题目背景 无 题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M≤15 ...

  9. 微软雅黑的Unicode码和英文名

    中文名 英文名 Unicode编码 微软雅黑 Microsoft YaHei \5FAE\8F6F\96C5\9ED1 宋 体 SimSun \5B8B\4F53 黑 体 SimHei \9ED1\4 ...

  10. Lucene4.x创建索引与3.x的一些不同

    lucene3.x的时候创建Field的时候可以直接指定存储和索引的选项类下下边这样: doc.add(new Field("createrId",diaryField.getCr ...