官网  这里使用的是最新 4.1.1版本

文档


解压  copy dist文件夹至工程目录  引入

  1. <link rel="stylesheet prefetch" href="${baseURL}/js/3rd-plug/PhotoSwipe-4.1.1/photoswipe.css">
  2. <link rel="stylesheet prefetch" href="${baseURL}/js/3rd-plug/PhotoSwipe-4.1.1/default-skin/default-skin.css">
  3. <script src="${baseURL}/js/3rd-plug/PhotoSwipe-4.1.1/photoswipe.js"></script>
  4. <script src="${baseURL}/js/3rd-plug/PhotoSwipe-4.1.1/photoswipe-ui-default.min.js"></script>

加上控件 所需要的html ,这样可以直接改html 使控件变样式 ,而不用配参数

  1. <!-- Root element of PhotoSwipe. Must have class pswp. -->
  2. <div class="pswp" tabindex="-1" role="dialog" aria-hidden="true" style="top: 40px;height: 95%;">
  3. <!-- Background of PhotoSwipe.
  4. It's a separate element as animating opacity is faster than rgba(). -->
  5. <div class="pswp__bg"></div>
  6. <!-- Slides wrapper with overflow:hidden. -->
  7. <div class="pswp__scroll-wrap">
  8. <!-- Container that holds slides.
  9. PhotoSwipe keeps only 3 of them in the DOM to save memory.
  10. Don't modify these 3 pswp__item elements, data is added later on. -->
  11. <div class="pswp__container">
  12. <div class="pswp__item"></div>
  13. <div class="pswp__item"></div>
  14. <div class="pswp__item"></div>
  15. </div>
  16. <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
  17. <div class="pswp__ui pswp__ui--hidden">
  18. <div class="pswp__top-bar">
  19. <!-- Controls are self-explanatory. Order can be changed. -->
  20. <div class="pswp__counter"></div>
  21. <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
  22. <button class="pswp__button pswp__button--share" title="Share"></button>
  23. <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
  24. <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
  25. <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
  26. <!-- element will get class pswp__preloader--active when preloader is running -->
  27. <div class="pswp__preloader">
  28. <div class="pswp__preloader__icn">
  29. <div class="pswp__preloader__cut">
  30. <div class="pswp__preloader__donut"></div>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
  36. <div class="pswp__share-tooltip"></div>
  37. </div>
  38. <button class="pswp__button pswp__button--arrow--left" style="visibility: visible" title="Previous (arrow left)">
  39. </button>
  40. <button class="pswp__button pswp__button--arrow--right" style="visibility: visible" title="Next (arrow right)">
  41. </button>
  42. <div class="pswp__caption">
  43. <div class="pswp__caption__center"></div>
  44. </div>
  45. </div>
  46. </div>
  47. </div>

这段html 可以配置控件的默认  如让箭头显示等样式


接下来配置初始化js   该JS是初始化一个相册列表 ,需要点击才能放大  , 当然可以模拟点击,具体看下面

  1. var initPhotoSwipeFromDOM = function(gallerySelector) {
  2. // parse slide data (url, title, size ...) from DOM elements
  3. // (children of gallerySelector)
  4. var parseThumbnailElements = function(el) {
  5. var thumbElements = el.childNodes,
  6. numNodes = thumbElements.length,
  7. items = [],
  8. figureEl,
  9. linkEl,
  10. size,
  11. item;
  12. for(var i = 0; i < numNodes; i++) {
  13. figureEl = thumbElements[i]; // <figure> element
  14. // include only element nodes
  15. if(figureEl.nodeType !== 1) {
  16. continue;
  17. }
  18. linkEl = figureEl.children[0]; // <a> element
  19. size = linkEl.getAttribute('data-size').split('x');
  20. // create slide object
  21. item = {
  22. src: linkEl.getAttribute('href'),
  23. w: parseInt(size[0], 10),
  24. h: parseInt(size[1], 10)
  25. };
  26. if(figureEl.children.length > 1) {
  27. // <figcaption> content
  28. item.title = figureEl.children[1].innerHTML;
  29. }
  30. if(linkEl.children.length > 0) {
  31. // <img> thumbnail element, retrieving thumbnail url
  32. item.msrc = linkEl.children[0].getAttribute('src');
  33. }
  34. item.el = figureEl; // save link to element for getThumbBoundsFn
  35. items.push(item);
  36. }
  37. return items;
  38. };
  39. // find nearest parent element
  40. var closest = function closest(el, fn) {
  41. return el && ( fn(el) ? el : closest(el.parentNode, fn) );
  42. };
  43. // triggers when user clicks on thumbnail
  44. var onThumbnailsClick = function(e) {
  45. e = e || window.event;
  46. e.preventDefault ? e.preventDefault() : e.returnValue = false;
  47. var eTarget = e.target || e.srcElement;
  48. // find root element of slide
  49. var clickedListItem = closest(eTarget, function(el) {
  50. return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
  51. });
  52. if(!clickedListItem) {
  53. return;
  54. }
  55. // find index of clicked item by looping through all child nodes
  56. // alternatively, you may define index via data- attribute
  57. var clickedGallery = clickedListItem.parentNode,
  58. childNodes = clickedListItem.parentNode.childNodes,
  59. numChildNodes = childNodes.length,
  60. nodeIndex = 0,
  61. index;
  62. for (var i = 0; i < numChildNodes; i++) {
  63. if(childNodes[i].nodeType !== 1) {
  64. continue;
  65. }
  66. if(childNodes[i] === clickedListItem) {
  67. index = nodeIndex;
  68. break;
  69. }
  70. nodeIndex++;
  71. }
  72. if(index >= 0) {
  73. // open PhotoSwipe if valid index found
  74. openPhotoSwipe( index, clickedGallery );
  75. }
  76. return false;
  77. };
  78. // parse picture index and gallery index from URL (#&pid=1&gid=2)
  79. var photoswipeParseHash = function() {
  80. var hash = window.location.hash.substring(1),
  81. params = {};
  82. if(hash.length < 5) {
  83. return params;
  84. }
  85. var vars = hash.split('&');
  86. for (var i = 0; i < vars.length; i++) {
  87. if(!vars[i]) {
  88. continue;
  89. }
  90. var pair = vars[i].split('=');
  91. if(pair.length < 2) {
  92. continue;
  93. }
  94. params[pair[0]] = pair[1];
  95. }
  96. if(params.gid) {
  97. params.gid = parseInt(params.gid, 10);
  98. }
  99. return params;
  100. };
  101. var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
  102. var pswpElement = document.querySelectorAll('.pswp')[0],
  103. gallery,
  104. options,
  105. items;
  106. items = parseThumbnailElements(galleryElement);
  107. // define options (if needed)
  108. options = {
  109. // define gallery index (for URL)
  110. galleryUID: galleryElement.getAttribute('data-pswp-uid'),
  111. getThumbBoundsFn: function(index) {
  112. // See Options -> getThumbBoundsFn section of documentation for more info
  113. var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
  114. pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
  115. rect = thumbnail.getBoundingClientRect();
  116. return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
  117. },
  118. closeEl:false,
  119. fullscreenEl : false, // 是否支持全屏按钮
  120. zoomEl: true,
  121. shareEl: false,
  122. counterEl: true,
  123. arrowEl: true,
  124. preloaderEl: true,
  125. closeOnScroll:false,
  126. arrowKeys:true
  127. };
  128. // PhotoSwipe opened from URL
  129. if(fromURL) {
  130. if(options.galleryPIDs) {
  131. // parse real index when custom PIDs are used
  132. // http://photoswipe.com/documentation/faq.html#custom-pid-in-url
  133. for(var j = 0; j < items.length; j++) {
  134. if(items[j].pid == index) {
  135. options.index = j;
  136. break;
  137. }
  138. }
  139. } else {
  140. // in URL indexes start from 1
  141. options.index = parseInt(index, 10) - 1;
  142. }
  143. } else {
  144. options.index = parseInt(index, 10);
  145. }
  146. // exit if index not found
  147. if( isNaN(options.index) ) {
  148. return;
  149. }
  150. if(disableAnimation) {
  151. options.showAnimationDuration = 0;
  152. }
  153. // Pass data to PhotoSwipe and initialize it
  154. gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
  155. gallery.init();
  156. };
  157. // loop through all gallery elements and bind events
  158. var galleryElements = document.querySelectorAll( gallerySelector );
  159. for(var i = 0, l = galleryElements.length; i < l; i++) {
  160. galleryElements[i].setAttribute('data-pswp-uid', i+1);
  161. galleryElements[i].onclick = onThumbnailsClick;
  162. }
  163. // Parse URL and open gallery if it contains #&pid=3&gid=1
  164. var hashData = photoswipeParseHash();
  165. if(hashData.pid && hashData.gid) {
  166. openPhotoSwipe( hashData.pid , galleryElements[ hashData.gid - 1 ], true, true );
  167. }
  168. };

具体参数可以看文档

准备自定义的html(从服务器获取的数据)

  1. <div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery" style="display: none">
  2. <c:forEach items="${requestScope.housePhotoes}" var="housePhoto" >
  3. <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
  4. <a href="${housePhoto.photourl}" itemprop="contentUrl" data-size="1024x1024">
  5. <img src="${housePhoto.photourl}" itemprop="thumbnail" alt="Image description" />
  6. </a>
  7. <figcaption itemprop="caption description">${housePhoto.photoDescrition}</figcaption>
  8. </figure>
  9. </c:forEach>
  10. </div>

调用初始化方法传入自定义的class name  并模拟点击

  1. initPhotoSwipeFromDOM('.my-gallery');
  2. $(function(){
  3. $(".my-gallery").find('figure')[0].click()
  4. })


OK 搞定





附件列表

js photoswipe 相册使用 移动pc端均可的更多相关文章

  1. [应用][js+css3]3D盒子导航[PC端]

    CSS3构建的3D盒子之导航应用 1.在用css3构建的盒子表面,放上iframe,来加载导航页面. 2.鼠标左键按下移动可旋转盒子,寻找想要的网址. 3.左键单机盒子表面,将全屏现实所点盒子表面的网 ...

  2. js判断用户是在PC端或移动端访问

    js如何判断用户是在PC端和还是移动端访问.  最近一直在忙我们团队的项目“咖啡之翼”,在这个项目中,我们为移动平台提供了一个优秀的体验.伴随Android平台的红火发展.不仅带动国内智能手机行业,而 ...

  3. 基于React.js网页版弹窗|react pc端自定义对话框组件RLayer

    基于React.js实现PC桌面端自定义弹窗组件RLayer. 前几天有分享一个Vue网页版弹框组件,今天分享一个最新开发的React PC桌面端自定义对话框组件. RLayer 一款基于react. ...

  4. js实现一个可以兼容PC端和移动端的div拖动效果

    前段时间写了一个简单的div拖动效果,不料昨天项目上正好需要一个相差不多的需求,就正好用上了,但是在移动端的时候却碰到了问题,拖动时候用到的三个事件:mousedown.mousemove.mouse ...

  5. JS判断android ios系统 PC端和移动端

    最近公司上线移动端,需要根据不同的系统跳转到不同的产品页面,百度后发现这一段代码很好用,不但可以判断当前是什么系统,还能知道当前浏览器是什么内核,移动端PC端都已测试无问题! var browser ...

  6. JS判断终端设备跳转PC端、移动端相应的URL

    <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta nam ...

  7. js判断当前浏览器是pc端还是移动端

    根据用户的访问设备的不同来显示不同的页面样式,主要是判断移动设备还是电脑浏览器访问的. 下面给出js判断处理代码,以作参考. <script type="text/javascript ...

  8. js判断是手机还是PC端

    有时接触一些手机上的适应,需要知道是pc 还是移动端 function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = [ ...

  9. js 判断移动设备、pc端、android、iPhone、是否为微信、微博、qq空间

    varbrowser = {   versions: function () {      var u = navigator.userAgent, app = navigator.appVersio ...

随机推荐

  1. 如何在MyEclipse中将项目部署Tomcat

    如果系统里面还没有配置Tomcat,请参照<Windows下如何配置tomcat环境变量>. 1.新建一个web project项目.如下: 效果如下: 2.编写一个实现Servlet接口 ...

  2. Web服务器性能压力测试工具http_load、webbench、ab、Siege使用教程

    Web服务器性能压力测试工具http_load.webbench.ab.Siege使用教程 作者: feng 日期: 2012/07/25 发表评论 (0) 查看评论   一.http_load 程序 ...

  3. Oracle-client支持exp|imp|rman

    官方精简版的驱动,不支持持exp/imp/rman,故需要安装oracle_client客户端. 实验环境: Centos6.5 x64   Oracle 11.2.0.4.0 Oracle_clie ...

  4. 【laravel5.4+vue.js】laravel 循环三维数组,解决:htmlentities() expects parameter 1 to be string, array given

    laravel循环三维数组   +++   vue.js循环三维数组  (数据均是以三维数组形式存在的) <form-item label="权限名称" prop=" ...

  5. PHP-Ajax跨域解决方案

    1.先了解下Ajax跨域问题: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht ...

  6. java 安装配置时出现的问题

    Error: could not open `C:\Program Files\Java\jre6\lib\i386\jvm.cfg') jdkerror  前些日子装了个jdk7试了试,后来做项目需 ...

  7. HDUOJ---Hamming Distance(4712)

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  8. Concurrency Managed Workqueue(四)workqueue如何处理work

    一.前言 本文主要讲述下面两部分的内容: 1.将work挂入workqueue的处理过程 2.如何处理挂入workqueue的work 二.用户将一个work挂入workqueue 1.queue_w ...

  9. Codeforces 6D Lizards and Basements 2 dfs+暴力

    题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

  10. 【Life】 Never Too Late, Just Do it Better!

    开这个博客: 一来是认为自己记忆力不好,对所学的东西做个记录: 二来是希望找到很多其它志同道合的人.一起交流进步: 不论什么时候開始努力都不晚! 希望平淡的工作生活不要磨灭我们心中的梦想,与君共勉~