js photoswipe 相册使用 移动pc端均可


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

文档

js photoswipe  相册使用     移动pc端均可
解压  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 搞定





附件列表

上一篇:初识JAVA(【面向对象】:pub/fri/pro/pri、封装/继承/多态、接口/抽象类、静态方法和抽象方法;泛型、垃圾回收机制、反射和RTTI)


下一篇:MapReduce 程序运行报错 java.lang.ClassNotFoundException解决方法