You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
8.5 KiB

2 years ago
  1. // Main/Product image slider for product page
  2. $('.main-img-slider').slick({
  3. slidesToShow: 1,
  4. slidesToScroll: 1,
  5. infinite: true,
  6. arrows: true,
  7. fade:true,
  8. speed: 300,
  9. lazyLoad: 'ondemand',
  10. asNavFor: '.thumb-nav',
  11. prevArrow: '<div class="slick-prev"><i class="i-chev-left-thin"></i><span class="sr-text">Previous</span></div>',
  12. nextArrow: '<div class="slick-next"><i class="i-chev-right-thin"></i><span class="sr-text">Next</span></div>'
  13. });
  14. // Thumbnail/alternates slider for product page
  15. $('.thumb-nav').slick({
  16. slidesToShow: 4,
  17. slidesToScroll: 1,
  18. infinite: true,
  19. centerPadding: '0px',
  20. asNavFor: '.main-img-slider',
  21. dots: false,
  22. centerMode: false,
  23. draggable: false,
  24. speed:200,
  25. focusOnSelect: true,
  26. prevArrow: '<div class="slick-prev"><i class="i-chev-left-thin"></i><span class="sr-text">Previous</span></div>',
  27. nextArrow: '<div class="slick-next"><i class="i-chev-right-thin"></i><span class="sr-text">Next</span></div>'
  28. });
  29. //keeps thumbnails active when changing main image, via mouse/touch drag/swipe
  30. $('.main-img-slider').on('afterChange', function(event, slick, currentSlide, nextSlide){
  31. //remove all active class
  32. $('.thumb-nav .slick-slide').removeClass('slick-current');
  33. //set active class for current slide
  34. $('.thumb-nav .slick-slide:not(.slick-cloned)').eq(currentSlide).addClass('slick-current');
  35. });
  36. //Photoswipe configuration for product page zoom
  37. var initPhotoSwipeFromDOM = function(gallerySelector) {
  38. // parse slide data (url, title, size ...) from DOM elements
  39. // (children of gallerySelector)
  40. var parseThumbnailElements = function(el) {
  41. var thumbElements = el.childNodes,
  42. numNodes = thumbElements.length,
  43. items = [],
  44. figureEl,
  45. linkEl,
  46. size,
  47. item;
  48. for(var i = 0; i < numNodes; i++) {
  49. figureEl = thumbElements[i]; // <figure> element
  50. // include only element nodes
  51. if(figureEl.nodeType !== 1) {
  52. continue;
  53. }
  54. linkEl = figureEl.children[0]; // <a> element
  55. size = linkEl.getAttribute('data-size').split('x');
  56. // create slide object
  57. item = {
  58. src: linkEl.getAttribute('href'),
  59. w: parseInt(size[0], 10),
  60. h: parseInt(size[1], 10)
  61. };
  62. if(figureEl.children.length > 1) {
  63. // <figcaption> content
  64. item.title = figureEl.children[1].innerHTML;
  65. }
  66. if(linkEl.children.length > 0) {
  67. // <img> thumbnail element, retrieving thumbnail url
  68. item.msrc = linkEl.children[0].getAttribute('src');
  69. }
  70. item.el = figureEl; // save link to element for getThumbBoundsFn
  71. items.push(item);
  72. }
  73. return items;
  74. };
  75. // find nearest parent element
  76. var closest = function closest(el, fn) {
  77. return el && ( fn(el) ? el : closest(el.parentNode, fn) );
  78. };
  79. // triggers when user clicks on thumbnail
  80. var onThumbnailsClick = function(e) {
  81. e = e || window.event;
  82. e.preventDefault ? e.preventDefault() : e.returnValue = false;
  83. var eTarget = e.target || e.srcElement;
  84. // find root element of slide
  85. var clickedListItem = closest(eTarget, function(el) {
  86. return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
  87. });
  88. if(!clickedListItem) {
  89. return;
  90. }
  91. // find index of clicked item by looping through all child nodes
  92. // alternatively, you may define index via data- attribute
  93. var clickedGallery = clickedListItem.parentNode,
  94. childNodes = clickedListItem.parentNode.childNodes,
  95. numChildNodes = childNodes.length,
  96. nodeIndex = 0,
  97. index;
  98. for (var i = 0; i < numChildNodes; i++) {
  99. if(childNodes[i].nodeType !== 1) {
  100. continue;
  101. }
  102. if(childNodes[i] === clickedListItem) {
  103. index = nodeIndex;
  104. break;
  105. }
  106. nodeIndex++;
  107. }
  108. if(index >= 0) {
  109. // open PhotoSwipe if valid index found
  110. openPhotoSwipe( index, clickedGallery );
  111. }
  112. return false;
  113. };
  114. // parse picture index and gallery index from URL (#&pid=1&gid=2)
  115. var photoswipeParseHash = function() {
  116. var hash = window.location.hash.substring(1),
  117. params = {};
  118. if(hash.length < 5) {
  119. return params;
  120. }
  121. var vars = hash.split('&');
  122. for (var i = 0; i < vars.length; i++) {
  123. if(!vars[i]) {
  124. continue;
  125. }
  126. var pair = vars[i].split('=');
  127. if(pair.length < 2) {
  128. continue;
  129. }
  130. params[pair[0]] = pair[1];
  131. }
  132. if(params.gid) {
  133. params.gid = parseInt(params.gid, 10);
  134. }
  135. return params;
  136. };
  137. var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
  138. var pswpElement = document.querySelectorAll('.pswp')[0],
  139. gallery,
  140. options,
  141. items;
  142. items = parseThumbnailElements(galleryElement);
  143. // define options (if needed)
  144. options = {
  145. // loop:false,
  146. // define gallery index (for URL)
  147. // barsSize : {top:140,bottom:0},
  148. captionEl : false,
  149. fullscreenEl : false,
  150. shareEl : false,
  151. bgOpacity : 1,
  152. tapToClose : true,
  153. tapToToggleControls : false,
  154. closeOnScroll: false,
  155. history:false,
  156. closeOnVerticalDrag:false,
  157. captionEl: false,
  158. fullscreenEl: false,
  159. zoomEl: false,
  160. shareEl: false,
  161. counterEl: false,
  162. arrowEl: true,
  163. galleryUID: galleryElement.getAttribute('data-pswp-uid'),
  164. getThumbBoundsFn: function(index) {
  165. // See Options -> getThumbBoundsFn section of documentation for more info
  166. var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
  167. pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
  168. rect = thumbnail.getBoundingClientRect();
  169. return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
  170. }
  171. };
  172. // PhotoSwipe opened from URL
  173. if(fromURL) {
  174. if(options.galleryPIDs) {
  175. // parse real index when custom PIDs are used
  176. // http://photoswipe.com/documentation/faq.html#custom-pid-in-url
  177. for(var j = 0; j < items.length; j++) {
  178. if(items[j].pid == index) {
  179. options.index = j;
  180. break;
  181. }
  182. }
  183. } else {
  184. // in URL indexes start from 1
  185. options.index = parseInt(index, 10) - 1;
  186. }
  187. } else {
  188. options.index = parseInt(index, 10);
  189. }
  190. // exit if index not found
  191. if( isNaN(options.index) ) {
  192. return;
  193. }
  194. if(disableAnimation) {
  195. options.showAnimationDuration = 0;
  196. }
  197. // Pass data to PhotoSwipe and initialize it
  198. gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
  199. gallery.init();
  200. var psIndex = gallery.getCurrentIndex();
  201. var psIndexSlick = psIndex;
  202. // console.log(psIndexSlick);
  203. gallery.listen('afterChange', function() {
  204. var psIndex = gallery.getCurrentIndex();
  205. var psIndexSlick = psIndex;
  206. // console.log(psIndexSlick);
  207. $(".main-img-slider").slick( "slickGoTo", psIndexSlick);
  208. });
  209. };
  210. var options = {
  211. loop: false
  212. };
  213. // loop through all gallery elements and bind events
  214. var galleryElements = document.querySelectorAll( gallerySelector );
  215. for(var i = 0, l = galleryElements.length; i < l; i++) {
  216. galleryElements[i].setAttribute('data-pswp-uid', i+1);
  217. galleryElements[i].onclick = onThumbnailsClick;
  218. }
  219. // Parse URL and open gallery if it contains #&pid=3&gid=1
  220. var hashData = photoswipeParseHash();
  221. if(hashData.pid && hashData.gid) {
  222. openPhotoSwipe( hashData.pid , galleryElements[ hashData.gid - 1 ], true, true );
  223. }
  224. };
  225. // execute above function
  226. initPhotoSwipeFromDOM('.product-images');