PhotoSwipe中文API(五)


Responsive Images

PhotoSwipe不支持<圖片>或srcset,因為它要求所定義的圖像的尺寸,並使用延遲加載。但是,隨着圖像動態加載,它很容易切換人士透露,即便是在舊的瀏覽器不支持srcset。
讓我們假設你只有“中等”的圖片和“原始”(“大”)的圖像。首先,你需要存儲在幻燈片對象的圖像的路徑和大小,例如像這樣:

 1 var items = [
 2 
 3     // Slide 1
 4     {
 5         mediumImage: {
 6             src: 'path/to/medium-image-1.jpg',
 7             w:800,
 8             h:600
 9         },
10         originalImage: {
11             src: 'path/to/large-image-1.jpg',
12             w: 1400,
13             h: 1050
14         }
15     },
16 
17     // Slide 2
18     // {
19     //     mediumImage: {
20     //         src: 'path/to/medium-image-2.jpg',
21     //         ...
22     //     
23     // ...
24 
25 ];

Then:

 1 // initialise as usual
 2 var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
 3 
 4 // create variable that will store real size of viewport
 5 var realViewportWidth,
 6     useLargeImages = false,
 7     firstResize = true,
 8     imageSrcWillChange;
 9 
10 // beforeResize event fires each time size of gallery viewport updates
11 gallery.listen('beforeResize', function() {
12     // gallery.viewportSize.x - width of PhotoSwipe viewport
13     // gallery.viewportSize.y - height of PhotoSwipe viewport
14     // window.devicePixelRatio - ratio between physical pixels and device independent pixels (Number)
15     //                          1 (regular display), 2 (@2x, retina) ...
16 
17 
18     // calculate real pixels when size changes
19     realViewportWidth = gallery.viewportSize.x * window.devicePixelRatio;
20 
21     // Code below is needed if you want image to switch dynamically on window.resize
22 
23     // Find out if current images need to be changed
24     if(useLargeImages && realViewportWidth < 1000) {
25         useLargeImages = false;
26         imageSrcWillChange = true;
27     } else if(!useLargeImages && realViewportWidth >= 1000) {
28         useLargeImages = true;
29         imageSrcWillChange = true;
30     }
31 
32     // Invalidate items only when source is changed and when it's not the first update
33     if(imageSrcWillChange && !firstResize) {
34         // invalidateCurrItems sets a flag on slides that are in DOM,
35         // which will force update of content (image) on window.resize.
36         gallery.invalidateCurrItems();
37     }
38 
39     if(firstResize) {
40         firstResize = false;
41     }
42 
43     imageSrcWillChange = false;
44 
45 });
46 
47 
48 // gettingData event fires each time PhotoSwipe retrieves image source & size
49 gallery.listen('gettingData', function(index, item) {
50 
51     // Set image source & size based on real viewport width
52     if( useLargeImages ) {
53         item.src = item.originalImage.src;
54         item.w = item.originalImage.w;
55         item.h = item.originalImage.h;
56     } else {
57         item.src = item.mediumImage.src;
58         item.w = item.mediumImage.w;
59         item.h = item.mediumImage.h;
60     }
61 
62     // It doesn't really matter what will you do here, 
63     // as long as item.src, item.w and item.h have valid values.
64     // 
65     // Just avoid http requests in this listener, as it fires quite often
66 
67 });
68 
69 
70 // Note that init() method is called after gettingData event is bound
71 gallery.init();

你不一定要使用幻燈片對象,看起來酷似以上(含mediumImage和largeImage對象)的結構。例如,您可以直接在圖像文件名(/path/to/large-image-600x500.jpg)存儲圖像的大小,然后在gettingData事件解析大小。只有item.src,item.w和item.h屬性由PhotoSwipe讀取和gettingData事件被觸發之后。

較大的圖像,不太流暢的動畫外觀。

盡量避免服務只是基於設備像素比或只是基於視口大小的圖像,始終兩者結合起來。

隨意的打開PhotoSwipe縮略圖使用srcset。

Image Gallery SEO

PhotoSwipe不強制HTML標記,你有完全控制權。簡單的標記是鏈接到大的圖像,最簡單的例子縮略圖列表:

1 <a href="large-image.jpg">
2     <img src="small-image.jpg" alt="Image description" />
3 </a>
4 ...

如果你有很長的標題,不適合在ALT或只是包含HTML標記,您可以使用<人物>和<figcaption>:

1 <figure>
2     <a href="large-image.jpg">
3         <img src="small-image.jpg" alt="Image description" />
4     </a>
5     <figcaption>Long image description</figcaption>
6 </figure>
7 ...

你可以更進一步,使用Schema.org標記為ImageGallery和ImageObject,它應該是這樣的:

 1 <div itemscope itemtype="http://schema.org/ImageGallery">
 2 
 3     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
 4         <a href="large-image.jpg" itemprop="contentUrl">
 5             <img src="small-image.jpg" itemprop="thumbnail" alt="Image description" />
 6         </a>
 7 
 8         <!-- optionally use this method to store image dimensions for PhotoSwipe -->
 9         <meta itemprop="width" content="300">
10         <meta itemprop="height" content="600">
11 
12         <figcaption itemprop="caption description">
13             Long image description
14 
15             <!-- optionally define copyright -->
16             <span itemprop="copyrightHolder">Photo: AP</span>
17         </figcaption>
18     </figure>
19 
20     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
21         <a href="large-image.jpg" itemprop="contentUrl">
22             <img src="small-image.jpg" itemprop="thumbnail" alt="Image description" />
23         </a>
24         <figcaption itemprop="caption description">Long image description</figcaption>
25     </figure>
26 
27     ...
28 
29 </div>

如果你不想縮略圖是網頁,例如可見你在畫廊50幅圖像,你只顯示前3的縮略圖+鏈接“查看所有圖片(50)”,你一定要在鏈接元素的內容使用Schema.org標記,你應該有所有50個鏈接(文字,而不是縮略圖)的DOM(你可能會顯示隱藏:無)。 例:

 1 <div itemscope itemtype="http://schema.org/ImageGallery">
 2 
 3     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
 4         <a href="large-image-1.jpg" itemprop="contentUrl">
 5             <figcaption itemprop="caption description">Long image description 1</figcaption>
 6         </a>
 7     </figure>
 8 
 9     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
10         <a href="large-image-2.jpg" itemprop="contentUrl">
11             <figcaption itemprop="caption description">Long image description 2</figcaption>
12         </a>
13     </figure>
14 
15     ...
16 
17 </div>

在上述所有情況下,大image.jpg文件將被完全索引。 - 不要關鍵字東西它沒有,只是不停的文本相關的,非垃圾郵件:即使你帶顯示隱藏的標題元素將被抓取。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM