智软工作室主要是为宁波公司提供宁波网站建设,宁波网站制作,宁波做网站公司,宁波外贸网站建设公司,宁波网站优化等项目,以优质的服务,优惠的价格,深的客户信赖。相信有你的加入我们的明天会更有动力!期待与你合作!
  • 宁波智软设计工作室,打造宁波网站制作工作室顶级品牌
  • 智软拥有专业的设计团队
  • 多年网站建设团队为你服务
  • 智软期待与你合作
  • 我们的技能
  • 我们掌握的技能有:dreamweaver,flash,photoshop
  • 宁波高端网站建设服务公司
    2014已经悄然到来,在新的一年我们恭贺所有客户事业蒸蒸日上,同事庆祝智软设计工作室成立5周年,在5周年为众多客户提供了保质量的网站设计与网站制作。 我们在新的一年质量与服务将会更加进一步为更多的客户带来高端的网站设计与服务!期待与你合作
  • 企业网站制作建设原因与服务介绍

    智软设计工作室多年为宁波企业网站制作建设服务一直以来我们以最优惠价格,最优质的质量,最突出的设计!得到了客户的一致认可,我们的服务包括:宁波网站设计,宁波网站建设,宁波网站制作,宁波网站优化,宁波英文网站制作我们会以优质的服务,最低的价格来报答客户!

  • 点击链接网站优化推广介绍

    点击连接网站优化推广-对企业的网站优化和网站推广--对企业的重要性-点击进行详细了解

  • 2014开幕,宁波网站设计服务全面优惠.

    2014开幕,宁波智软设计为宁波企业提供更加优秀的设计和实惠的价格

  • 链接到网站案例展示平台
    智软设计工作为宁波客户网站设计案例展示平台-案例均为定期更新,让新老客户更好的看到我们做的案例,点击查看我们做的网站案例吧!
  • 我们宁波网络公司提供网站一站式解决方案
    智软(宁波网络公司)帮你轻松解决网络问题,网站一站式解决方案
你当前的位置:智软工作室 > 营销与网络 >

photoswipe的展示时图片自适应问题 data-size自动获取问题

2021-01-06 15:32

 

  1. <!DOCTYPE html> 
  2. <html lang="en" > 
  3.  
  4. <head> 
  5.     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, viewport-fit=cover"> 
  6.   <meta charset="UTF-8"> 
  7.    
  8.  
  9.  
  10.   <title>CodePen - Multiple PhotoSwipe galleries on pagea</title> 
  11.    
  12.    
  13.   <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/photoswipe.min.css'> 
  14. <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/default-skin/default-skin.min.css'> 
  15.    
  16. <style> 
  17. .my-gallery { 
  18.   width: 100%; 
  19.   float: left; 
  20. } 
  21. .my-gallery img { 
  22.   width: 100%; 
  23.   height: auto; 
  24. } 
  25. .my-gallery figure { 
  26.   display: block; 
  27.   float: left; 
  28.   margin: 0 5px 5px 0; 
  29.   width: 150px; 
  30. } 
  31. .my-gallery figcaption { 
  32.   display: none; 
  33. } 
  34. </style> 
  35.  
  36.   <script> 
  37.   windowwindow.console = window.console || function(t) {}; 
  38. </script> 
  39.  
  40.    
  41.    
  42.   <script> 
  43.   if (document.location.search.match(/type=embed/gi)) { 
  44.     window.parent.postMessage("resize", "*"); 
  45.   } 
  46. </script> 
  47.  
  48.  
  49. </head> 
  50.  
  51. <body translate="no" > 
  52.   
  53.    
  54.    
  55.    
  56.    
  57.   <h2>4Second gallery:</h2> 
  58.  
  59.   <div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery"> 
  60.  
  61.    
  62.  
  63.     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"> 
  64.       <a href="https://www..tw/WebUpload/UpLoadFile/2021161173814.jpg" itemprop="contentUrl" data-size=""> 
  65.           <img src="https://www..tw/WebUpload/UpLoadFile/2021161173814.jpg" itemprop="thumbnail" alt="Image description" /> 
  66.       </a> 
  67.       <figcaption itemprop="caption description">ddd2.1</figcaption> 
  68.     </figure> 
  69.  
  70.     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"> 
  71.       <a href="https://www..tw/WebUpload/UpLoadFile/2021162887102.jpg" itemprop="contentUrl" data-size=""> 
  72.           <img src="https://www..tw/WebUpload/UpLoadFile/2021162887102.jpg" itemprop="thumbnail" alt="Image description" /> 
  73.       </a> 
  74.       <figcaption itemprop="caption description">ee 2.2</figcaption> 
  75.     </figure> 
  76.  
  77.  
  78.   </div> 
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. <!-- Root element of PhotoSwipe. Must have class pswp. --> 
  103. <div class="pswp" tabindex="-1" role="dialog" aria-hidden="true"> 
  104.  
  105.     <!-- Background of PhotoSwipe.  
  106.          It's a separate element, as animating opacity is faster than rgba(). --> 
  107.     <div class="pswp__bg"></div> 
  108.  
  109.     <!-- Slides wrapper with overflow:hidden. --> 
  110.     <div class="pswp__scroll-wrap"> 
  111.  
  112.         <!-- Container that holds slides. PhotoSwipe keeps only 3 slides in DOM to save memory. --> 
  113.         <!-- don't modify these 3 pswp__item elements, data is added later on. --> 
  114.         <div class="pswp__container"> 
  115.             <div class="pswp__item"></div> 
  116.             <div class="pswp__item"></div> 
  117.             <div class="pswp__item"></div> 
  118.         </div> 
  119.  
  120.         <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. --> 
  121.         <div class="pswp__ui pswp__ui--hidden"> 
  122.  
  123.             <div class="pswp__top-bar"> 
  124.  
  125.                 <!--  Controls are self-explanatory. Order can be changed. --> 
  126.  
  127.                 <div class="pswp__counter"></div> 
  128.  
  129.                 <button class="pswp__button pswp__button--close" title="Close (Esc)"></button> 
  130.  
  131.                 <button class="pswp__button pswp__button--share" title="Share"></button> 
  132.  
  133.                 <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button> 
  134.  
  135.                 <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button> 
  136.  
  137.                 <!-- Preloader demo https://codepen.io/dimsemenov/pen/yyBWoR --> 
  138.                 <!-- element will get class pswp__preloader--active when preloader is running --> 
  139.                 <div class="pswp__preloader"> 
  140.                     <div class="pswp__preloader__icn"> 
  141.                       <div class="pswp__preloader__cut"> 
  142.                         <div class="pswp__preloader__donut"></div> 
  143.                       </div> 
  144.                     </div> 
  145.                 </div> 
  146.             </div> 
  147.  
  148.             <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap"> 
  149.                 <div class="pswp__share-tooltip"></div>  
  150.             </div> 
  151.  
  152.             <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)"> 
  153.             </button> 
  154.  
  155.             <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)"> 
  156.             </button> 
  157.  
  158.             <div class="pswp__caption"> 
  159.                 <div class="pswp__caption__center"></div> 
  160.             </div> 
  161.  
  162.           </div> 
  163.  
  164.         </div> 
  165.  
  166. </div> 
  167.     <script src="https://cpwebassets.codepen.io/assets/common/stopExecutionOnTimeout-157cd5b220a5c80d4ff8e0e70ac069bffd87a61252088146915e8726e5d9f147.js"></script> 
  168.  
  169.   <script src='https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/photoswipe.min.js'></script> 
  170. <script src='https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/photoswipe-ui-default.min.js'></script> 
  171.  
  172. <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script> 
  173.  
  174.       <script id="rendered-js" > 
  175. var initPhotoSwipeFromDOM = function (gallerySelector) { 
  176.  
  177.   // parse slide data (url, title, size ...) from DOM elements  
  178.   // (children of gallerySelector) 
  179.   var parseThumbnailElements = function (el) { 
  180.     var thumbElements = el.childNodes, 
  181.     numNodes = thumbElements.length, 
  182.     items = [], 
  183.     figureEl, 
  184.     linkEl, 
  185.     size, 
  186.     item; 
  187.  
  188.     for (var i = 0; i < numNodes; i++) {if (window.CP.shouldStopExecution(0)) break; 
  189.  
  190.       figureEl = thumbElements[i]; // <figure> element 
  191.  
  192.       // include only element nodes  
  193.       if (figureEl.nodeType !== 1) { 
  194.         continue; 
  195.       } 
  196.  
  197.       linkEl = figureEl.children[0]; // <a> element 
  198.  
  199.  
  200.       size = linkEl.getAttribute('data-size').split('x'); 
  201.  
  202.       // create slide object 
  203.       item = { 
  204.         src: linkEl.getAttribute('href'), 
  205.         w: parseInt(size[0], 10), 
  206.         h: parseInt(size[1], 10) }; 
  207.  
  208.  
  209.  
  210.  
  211.       if (figureEl.children.length > 1) { 
  212.         // <figcaption> content 
  213.         item.title = figureEl.children[1].innerHTML; 
  214.       } 
  215.  
  216.       if (linkEl.children.length > 0) { 
  217.         // <img> thumbnail element, retrieving thumbnail url 
  218.         item.msrc = linkEl.children[0].getAttribute('src'); 
  219.       } 
  220.  
  221.       item.el = figureEl; // save link to element for getThumbBoundsFn 
  222.       items.push(item); 
  223.     }window.CP.exitedLoop(0); 
  224.  
  225.     return items; 
  226.   }; 
  227.  
  228.   // find nearest parent element 
  229.   var closest = function closest(el, fn) { 
  230.     return el && (fn(el) ? el : closest(el.parentNode, fn)); 
  231.   }; 
  232.  
  233.   // triggers when user clicks on thumbnail 
  234.   var onThumbnailsClick = function (e) { 
  235.     ee = e || window.event; 
  236.     e.preventDefault ? e.preventDefault() : e.returnValue = false; 
  237.  
  238.     var eeTarget = e.target || e.srcElement; 
  239.  
  240.     // find root element of slide 
  241.     var clickedListItem = closest(eTarget, function (el) { 
  242.       return el.tagName && el.tagName.toUpperCase() === 'FIGURE'; 
  243.     }); 
  244.  
  245.     if (!clickedListItem) { 
  246.       return; 
  247.     } 
  248.  
  249.     // find index of clicked item by looping through all child nodes 
  250.     // alternatively, you may define index via data- attribute 
  251.     var clickedGallery = clickedListItem.parentNode, 
  252.     childNodes = clickedListItem.parentNode.childNodes, 
  253.     numChildNodes = childNodes.length, 
  254.     nodeIndex = 0, 
  255.     index; 
  256.  
  257.     for (var i = 0; i < numChildNodes; i++) {if (window.CP.shouldStopExecution(1)) break; 
  258.       if (childNodes[i].nodeType !== 1) { 
  259.         continue; 
  260.       } 
  261.  
  262.       if (childNodes[i] === clickedListItem) { 
  263.         index = nodeIndex; 
  264.         break; 
  265.       } 
  266.       nodeIndex++; 
  267.     }window.CP.exitedLoop(1); 
  268.  
  269.  
  270.  
  271.     if (index >= 0) { 
  272.       // open PhotoSwipe if valid index found 
  273.       openPhotoSwipe(index, clickedGallery); 
  274.     } 
  275.     return false; 
  276.   }; 
  277.  
  278.   // parse picture index and gallery index from URL (#&pid=1&gid=2) 
  279.   var photoswipeParseHash = function () { 
  280.     var hash = window.location.hash.substring(1), 
  281.     params = {}; 
  282.  
  283.     if (hash.length < 5) { 
  284.       return params; 
  285.     } 
  286.  
  287.     var vars = hash.split('&'); 
  288.     for (var i = 0; i < vars.length; i++) {if (window.CP.shouldStopExecution(2)) break; 
  289.       if (!vars[i]) { 
  290.         continue; 
  291.       } 
  292.       var pair = vars[i].split('='); 
  293.       if (pair.length < 2) { 
  294.         continue; 
  295.       } 
  296.       params[pair[0]] = pair[1]; 
  297.     }window.CP.exitedLoop(2); 
  298.  
  299.     if (params.gid) { 
  300.       params.gid = parseInt(params.gid, 10); 
  301.     } 
  302.  
  303.     return params; 
  304.   }; 
  305.  
  306.   var openPhotoSwipe = function (index, galleryElement, disableAnimation, fromURL) { 
  307.     var pswpElement = document.querySelectorAll('.pswp')[0], 
  308.     gallery, 
  309.     options, 
  310.     items; 
  311.  
  312.     items = parseThumbnailElements(galleryElement); 
  313.  
  314.     // define options (if needed) 
  315.     options = { 
  316.  
  317.       // define gallery index (for URL) 
  318.       galleryUID: galleryElement.getAttribute('data-pswp-uid'), 
  319.  
  320.       getThumbBoundsFn: function (index) { 
  321.         // See Options -> getThumbBoundsFn section of documentation for more info 
  322.         var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail 
  323.         pageYScroll = window.pageYOffset || document.documentElement.scrollTop, 
  324.         rect = thumbnail.getBoundingClientRect(); 
  325.  
  326.         return { x: rect.left, y: rect.top + pageYScroll, w: rect.width }; 
  327.       } }; 
  328.  
  329.  
  330.  
  331.     // PhotoSwipe opened from URL 
  332.     if (fromURL) { 
  333.       if (options.galleryPIDs) { 
  334.         // parse real index when custom PIDs are used  
  335.         // http://photoswipe.com/documentation/faq.html#custom-pid-in-url 
  336.         for (var j = 0; j < items.length; j++) {if (window.CP.shouldStopExecution(3)) break; 
  337.           if (items[j].pid == index) { 
  338.             options.index = j; 
  339.             break; 
  340.           } 
  341.         }window.CP.exitedLoop(3); 
  342.       } else { 
  343.         // in URL indexes start from 1 
  344.         options.index = parseInt(index, 10) - 1; 
  345.       } 
  346.     } else { 
  347.       options.index = parseInt(index, 10); 
  348.     } 
  349.  
  350.     // exit if index not found 
  351.     if (isNaN(options.index)) { 
  352.       return; 
  353.     } 
  354.  
  355.     if (disableAnimation) { 
  356.       options.showAnimationDuration = 0; 
  357.     } 
  358.  
  359.     // Pass data to PhotoSwipe and initialize it 
  360.     gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options); 
  361.     gallery.init(); 
  362.   }; 
  363.  
  364.   // loop through all gallery elements and bind events 
  365.   var galleryElements = document.querySelectorAll(gallerySelector); 
  366.  
  367.   for (var i = 0, l = galleryElements.length; i < l; i++) {if (window.CP.shouldStopExecution(4)) break; 
  368.     galleryElements[i].setAttribute('data-pswp-uid', i + 1); 
  369.     galleryElements[i].onclick = onThumbnailsClick; 
  370.   } 
  371.  
  372.   // Parse URL and open gallery if it contains #&pid=3&gid=1 
  373.   window.CP.exitedLoop(4);var hashData = photoswipeParseHash(); 
  374.   if (hashData.pid && hashData.gid) { 
  375.     openPhotoSwipe(hashData.pid, galleryElements[hashData.gid - 1], true, true); 
  376.   } 
  377. }; 
  378.  
  379. // execute above function 
  380. initPhotoSwipeFromDOM('.my-gallery'); 
  381. //# sourceURL=pen.js 
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  /**重点。直接利用jq加载,自动获取图片得 data-size 得参数 */
  394.  
  395. window.onload=function(){ 
  396. auto_data_size(); 
  397. }; 
  398. function auto_data_size(){ 
  399. var imgss= $("figure img"); 
  400. $("figure img").each(function() { 
  401. var imgs = new Image(); 
  402. imgs.src=$(this).attr("src"); 
  403. var w = imgs.width, 
  404. h =imgs.height; 
  405. $(this).parent("a").attr("data-size","").attr("data-size",w+"x"+h); 
  406. }) 
  407. }; 
  408.     </script> 
  409.  
  410.    
  411. </body> 
  412.  
  413. </html> 
  414.   

 

更多

【责任编辑:小戴工程师】

上一篇:Photoshop CS6 打字不能打出空格和出现字体无法完成请求因为程序 下一篇:dedecms分类信息模型 实现自定义联动字段和普通字段多选条件搜索

最新文章

2021-01-06 15:32

相关文章

2021-01-06 15:32
网站制作咨询:
QQ:277728291
电话:18668277799
邮箱:277728291@qq.com
期待你来电咨询
回顶部
Copyright 2010 智软工作室 版权所有    智软为宁波公司提供最优惠的网络服务【宁波网站建设,宁波网站制作公司,宁波做网站公司,宁波外贸网站建设公司】我们竭诚为你服务!