1. 程式人生 > >PhotoSwipe 官方API解讀(一)

PhotoSwipe 官方API解讀(一)

一:getting started
開始之前要知道:
1、 PhotoSwipe需要提前預定義圖片的大小(more: http://photoswipe.com/documentation/faq.html#image-size
2、 如果您把PhotoSwipe用在非響應式的站點上,但是這個控制元件也會在移動端進行縮放(因為這整個頁面就是縮放的)。所以你需要實現自定義控制元件(例如單大關閉按鈕在右上角)。
3、 文件中的所有程式碼都是純JS,支援IE 8和以上。如果你的網站或應用程式使用一些JavaScript框架(如jQuery和MooTools)或你不需要支援舊的瀏覽器——隨意簡化程式碼。
4、 避免使用大圖片(大於2000 x1500px)於移動端上,因為他們將大大減少動畫表現並且可能導致事故(特別是在iOS Safari)。可能的解決方案: 

http://photoswipe.com/documentation/responsive-images.html,或開啟影象在一個單獨的頁面,或使用庫,支援影象鑲嵌 (比如http://leafletjs.com/)(more:http://photoswipe.com/documentation/faq.html#mobile-crash)。

安裝:
第一步:引入js css

<!-- 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()的時候程式碼才會執行。因此,如果你不需要在一開始就開啟檔案的話,就可以延遲載入檔案。

第二步:向DOM新增PhotoSwipe (.pswp)元素
您可以直接在初始化之前通過JS動態地新增HTML程式碼,或在頁面最初的時候(例如演示頁面上那樣)。這段程式碼可以在任何地方附加,但最好在</body>標籤關閉之前。您可以在多個庫中重用它(只要使用相同的UI類)。

<!-- 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 elements 的順序不能變。
你可能會問,為什麼PhotoSwipe沒有通過JS自動新增這段程式碼,原因很簡單——只是為了儲存檔案大小,以防你需要修改佈局。

第三步:初始化
執行PhotoSwipe建構函式,它包含4個引數
.pswp element from step 2 (it must be added to DOM).
PhotoSwipe UI class. If you included default photoswipe-ui-default.js, class will be PhotoSwipeUI_Default. Can be false.
Array with objects (slides).
Options.
1、.pswp -----第二步中所指的元素(它必須被新增到DOM中):
2、PhotoSwipe UI類 ------如果你引入了預設的photoswipe-ui-default.js, class會是PhotoSwipeUI_Default(這句沒翻譯通順)
3、需要滑動的陣列(slides)
4、配置項

var pswpElement = document.querySelectorAll('.pswp')[0];
// build items array
var items = [
    {
        src: 'https://placekitten.com/600/400',
        w: 600,
        h: 400
    },
    {
        src: 'https://placekitten.com/1200/900',
        w: 1200,
        h: 900
    }
];
// define options (if needed)
var options = {
    // optionName: 'option value'
    // for example:
    index: 0 // start at first slide
};
// Initializes and opens PhotoSwipe
var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();

寫完以上這些,則可以得到以下結果

U_EN5XA3J6EI}I{CR8NXEFD.png



作者:ice小末
連結:https://www.jianshu.com/p/c7d2108cecde
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯絡作者獲得授權並註明出處。