PhotoSwipe入门(2)
原文地址,點擊直達,閱讀效果更佳
開始之前
- PhotoSwipe不像jquery插件那樣簡單。你需要有一些javascript基礎。
- PhotoSwipe需要預定義圖片的尺寸(more about this)
- 如果你在非響應式網站上使用PhotoSwipe,控制器將會被縮放(基于整個頁面縮放)。因此你需要自己實現控制器(如右上角的關閉按鈕)
- 插件中的所有代碼是原汁原味的js并且支持IE8及其以上。如果你的網站或應用使用一些javascript框架(如jQuery或MooTools)或你不需要支持老的瀏覽器,你可以自由的簡化插件代碼。
- 避免為移動設備提供大的圖片(大于2000X1500px),它們會非常靈異的降低動畫性能,更有甚,會引起崩潰,特別是在IOS系統上的safari瀏覽器上。解決方案:提供響應式圖片或在單獨的頁面打開圖片或使用支持圖片響應的庫(像 Leaflet)
- 更多問題,請參照快問快答頁面(more in FAQ)
初始化
第一步:引入JS和CSS文件
你可以在 dist 目錄 或 Github倉庫 中找到他們。sass 和 編譯后的js文件在src目錄下,如果你對默認的UI樣式或代碼結構或注釋不滿意,我推薦你使用sass。
<!-- Core CSS file --> <link rel="stylesheet" href="path/to/photoswipe.css"> <!-- Skin CSS file (styling of UI - buttons, caption, etc.)In the folder of skin CSS file there are also:- .png and .svg icons sprite, - preloader.gif (for browsers that do not support CSS animations) --> <link rel="stylesheet" href="path/to/default-skin/default-skin.css"> <!-- Core JS file --> <script src="path/to/photoswipe.min.js"></script> <!-- UI JS file --> <script src="path/to/photoswipe-ui-default.min.js"></script>不管你怎么引入或在什么地方JS和CSS文件,代碼執行僅僅需要new PhotoSwipe(),所以,你可以在你需要的時候加載文件。
PhotoSwipe也支持AMD加載方式(如:RequireJS)和CommonJS,你可以像下面這樣使用他們:
require(['path/to/photoswipe.js','path/to/photoswipe-ui-default.js', ],function(PhotoSwipe, PhotoSwipeUI_default){//var gallery = new PhotoSwipe( someElement, PhotoSwipeUI_Default ...// gallery.init() // ... })你也可以通過Bower(bower install photoswipe)或NPM(npm install photoswipe)安裝PhotoSwipe
第二步:添加PhotoSwipe(.pswp)元素到DOM結構中
你可以通過js動態的添加HTML代碼(在PhotoSwipe初始化之前),或在頁面加載的時候,就把photoswipe(.pswp)元素寫在頁面中隨頁面一起加載。這段代碼可以在任何地方添加,比較理想的是放在body元素中。你可以在多個畫廊中使用這段代碼,但是必須保證有相同的CSSclass,
<!-- Root element of PhotoSwipe. Must have class pswp. --> <div class="pswp" tabindex="-1" role="dialog" aria-hidden="true"><!-- Background of PhotoSwipe. It's a separate element as animating opacity is faster than rgba(). --><div class="pswp__bg"></div><!-- Slides wrapper with overflow:hidden. --><div class="pswp__scroll-wrap"><!-- Container that holds slides. PhotoSwipe keeps only 3 of them in the DOM to save memory.Don't modify these 3 pswp__item elements, data is added later on. --><div class="pswp__container"><div class="pswp__item"></div><div class="pswp__item"></div><div class="pswp__item"></div></div><!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. --><div class="pswp__ui pswp__ui--hidden"><div class="pswp__top-bar"><!-- Controls are self-explanatory. Order can be changed. --><div class="pswp__counter"></div><button class="pswp__button pswp__button--close" title="Close (Esc)"></button><button class="pswp__button pswp__button--share" title="Share"></button><button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button><button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button><!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR --><!-- element will get class pswp__preloader--active when preloader is running --><div class="pswp__preloader"><div class="pswp__preloader__icn"><div class="pswp__preloader__cut"><div class="pswp__preloader__donut"></div></div></div></div></div><div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap"><div class="pswp__share-tooltip"></div> </div><button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)"></button><button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)"></button><div class="pswp__caption"><div class="pswp__caption__center"></div></div></div></div></div>pswp__bg,pswp__scroll-wrap,pswp__container,pswp__item元素的順序不許改變。
你可能會問:為什么PhotoSwipe不能動態的添加這段代碼
原因也很簡單:縮減文件大小,你可能會更改一些布局。
第三步:插件初始化
執行PhotoSwipe構造函數,它接收四個參數:
然后,就出效果了,如下,你可全屏查看效果:
如果僅僅為了用而用,那么到此就不用繼續向下看了
Array(數組) of Slide Objects
數組中的每個對象應該包含slide數據。它可以是你希望展示在PhotoSwipe中的任何東西。
像:圖片地址,標題,分享條目,評論等
默認情況下PhotoSwipe使用5個屬性:
- src: 圖片的地址
- w:圖片寬度
- h:圖片高度
- msrc:小圖片地址占位符
- html:自定義html(more about this)
在圖片導航過程中,PhotoSwipe會自添加屬性到這個 slide 對象上(如:minZoom,loaded)
var slides = [// slide 1{src: 'path/to/image1.jpg', // path to imagew: 1024, // image widthh: 768, // image heightmsrc: 'path/to/small-image.jpg', // small image placeholder,// main (large) image loads on top of it,// if you skip this parameter - grey rectangle will be displayed,// try to define this property only when small image was loaded beforetitle: 'Image Caption' // used by Default PhotoSwipe UI// if you skip it, there won't be any caption// You may add more properties here and use them.// For example, demo gallery uses "author" property, which is used in the caption.// author: 'John Doe'},// slide 2{src: 'path/to/image2.jpg', w: 600, h: 600// etc.}// etc.];在PhotoSwipe使用數據之前,你可以直接的動態地定義每個slide屬性。使用gettingData事件,關于這部分更多請移步到API Section of docs。例如:你可以使用這種技術為不同的屏幕提供不同尺寸的圖片。
如何構建一個數組的幻燈片鏈接列表
假設你有一個鏈接列表/縮略圖是這樣的:關于畫廊標簽的更多信息,SEO優化
<div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery"><figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"><a href="large-image.jpg" itemprop="contentUrl" data-size="600x400"><img src="small-image.jpg" itemprop="thumbnail" alt="Image description" /></a><figcaption itemprop="caption description">Image caption</figcaption></figure><figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"><a href="large-image.jpg" itemprop="contentUrl" data-size="600x400"><img src="small-image.jpg" itemprop="thumbnail" alt="Image description" /></a><figcaption itemprop="caption description">Image caption</figcaption></figure></div>如果你需要點擊縮略圖打開大圖片圖庫
你需要做到如下幾點:
PhotoSwipe不會關心你如何做到上面這些。如果你使用jQuery庫或MooTools庫,又或者你不需要支持IE8,那么代碼將會極其簡潔。
下面是原生js實現的,支持IE8:
Example on CodePen (focus & history options are disabled due to embed issues):
提示:你可能需要從CodePen下載例子在本地跑一跑。(Edit on CodePen -> Share -> Export .zip))
- 如果你需要使用不同的標簽(自定義UI界面),那么你需要編輯功能函數parseThumbnailElements
- 如果你對原生js經驗不足或不知道如何解析DOM,請參考:quirksmode和documentation on MDN.
- IE8不支持html5標簽<figure>和<figcation>元素。你需要在<head>標簽中引入html5shiv,如下:
總結
以上是生活随笔為你收集整理的PhotoSwipe入门(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis中resultMap详解
- 下一篇: 桌面倒计时(中考,高考,考研,生日等重要