一:getting started
開始之前要知道:
1尚镰、 PhotoSwipe需要提前預(yù)定義圖片的大小(more: http://photoswipe.com/documentation/faq.html#image-size)
2萨螺、 如果您把PhotoSwipe用在非響應(yīng)式的站點上,但是這個控件也會在移動端進行縮放(因為這整個頁面就是縮放的)。所以你需要實現(xiàn)自定義控件(例如單大關(guān)閉按鈕在右上角)主到。
3、 文檔中的所有代碼都是純JS躯概,支持IE 8和以上登钥。如果你的網(wǎng)站或應(yīng)用程序使用一些JavaScript框架(如jQuery和MooTools)或你不需要支持舊的瀏覽器——隨意簡化代碼。
4娶靡、 避免使用大圖片(大于2000 x1500px)于移動端上,因為他們將大大減少動畫表現(xiàn)并且可能導(dǎo)致事故(特別是在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文件都無關(guān)緊要结执。只有當你寫new PhotoSwipe()的時候代碼才會執(zhí)行。因此艾凯,如果你不需要在一開始就打開文件的話献幔,就可以延遲加載文件。
第二步:向DOM添加PhotoSwipe (.pswp)元素
您可以直接在初始化之前通過JS動態(tài)地添加HTML代碼,或在頁面最初的時候(例如演示頁面上那樣)趾诗。這段代碼可以在任何地方附加,但最好在</body>標簽關(guān)閉之前蜡感。您可以在多個庫中重用它(只要使用相同的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自動添加這段代碼,原因很簡單——只是為了保存文件大小贝乎,以防你需要修改布局情连。
第三步:初始化
執(zhí)行PhotoSwipe構(gòu)造函數(shù),它包含4個參數(shù)
.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虫几、需要滑動的數(shù)組(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();
寫完以上這些挽拔,則可以得到以下結(jié)果