日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PhotoSwipe入门(2)

發(fā)布時間:2024/3/24 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PhotoSwipe入门(2) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原文地址,點擊直達,閱讀效果更佳

開始之前

  • PhotoSwipe不像jquery插件那樣簡單。你需要有一些javascript基礎(chǔ)。
  • PhotoSwipe需要預(yù)定義圖片的尺寸(more about this)
  • 如果你在非響應(yīng)式網(wǎng)站上使用PhotoSwipe,控制器將會被縮放(基于整個頁面縮放)。因此你需要自己實現(xiàn)控制器(如右上角的關(guān)閉按鈕)
  • 插件中的所有代碼是原汁原味的js并且支持IE8及其以上。如果你的網(wǎng)站或應(yīng)用使用一些javascript框架(如jQuery或MooTools)或你不需要支持老的瀏覽器,你可以自由的簡化插件代碼。
  • 避免為移動設(shè)備提供大的圖片(大于2000X1500px),它們會非常靈異的降低動畫性能,更有甚,會引起崩潰,特別是在IOS系統(tǒng)上的safari瀏覽器上。解決方案:提供響應(yīng)式圖片或在單獨的頁面打開圖片或使用支持圖片響應(yīng)的庫(像 Leaflet)
  • 更多問題,請參照快問快答頁面(more in FAQ)

初始化

第一步:引入JS和CSS文件

你可以在 dist 目錄 或 Github倉庫 中找到他們。sass 和 編譯后的js文件在src目錄下,如果你對默認的UI樣式或代碼結(jié)構(gòu)或注釋不滿意,我推薦你使用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文件,代碼執(zhí)行僅僅需要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結(jié)構(gòu)中

你可以通過js動態(tài)的添加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不能動態(tài)的添加這段代碼
原因也很簡單:縮減文件大小,你可能會更改一些布局。

第三步:插件初始化

執(zhí)行PhotoSwipe構(gòu)造函數(shù),它接收四個參數(shù):

  • 第二步中.pswp元素必須添加到DOM中
  • 提供PhotoSwipeUI class。如果你引入了photoswipe-ui-default.js,那么UI類將會是PhotoSwipeUI_Default。否則是false。
  • 包含對象的數(shù)組(slides)
  • options
  • 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();

    然后,就出效果了,如下,你可全屏查看效果:

    如果僅僅為了用而用,那么到此就不用繼續(xù)向下看了

    Array(數(shù)組) of Slide Objects

    數(shù)組中的每個對象應(yīng)該包含slide數(shù)據(jù)。它可以是你希望展示在PhotoSwipe中的任何東西。
    像:圖片地址,標題,分享條目,評論等

    默認情況下PhotoSwipe使用5個屬性:

    • src: 圖片的地址
    • w:圖片寬度
    • h:圖片高度
    • msrc:小圖片地址占位符
    • html:自定義html(more about this)

    在圖片導(dǎo)航過程中,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使用數(shù)據(jù)之前,你可以直接的動態(tài)地定義每個slide屬性。使用gettingData事件,關(guān)于這部分更多請移步到API Section of docs。例如:你可以使用這種技術(shù)為不同的屏幕提供不同尺寸的圖片。

    如何構(gòu)建一個數(shù)組的幻燈片鏈接列表

    假設(shè)你有一個鏈接列表/縮略圖是這樣的:關(guān)于畫廊標簽的更多信息,SEO優(yōu)化

    <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>

    如果你需要點擊縮略圖打開大圖片圖庫

    你需要做到如下幾點:

  • 把鏈接/縮略圖與click事件綁定
  • 用戶點擊縮略圖后,獲取縮略圖的索引值
  • 創(chuàng)建幻燈片對象數(shù)組。遍歷DOM元素獲取所有的鏈接和檢索 href 屬性(大型圖像url), data-size屬性, 縮略圖的 src 和標題。
  • PhotoSwipe不會關(guān)心你如何做到上面這些。如果你使用jQuery庫或MooTools庫,又或者你不需要支持IE8,那么代碼將會極其簡潔。
    下面是原生js實現(xiàn)的,支持IE8:

    var initPhotoSwipeFromDOM = function(gallerySelector) {// parse slide data (url, title, size ...) from DOM elements // (children of gallerySelector)var parseThumbnailElements = function(el) {var thumbElements = el.childNodes,numNodes = thumbElements.length,items = [],figureEl,linkEl,size,item;for(var i = 0; i < numNodes; i++) {figureEl = thumbElements[i]; // <figure> element// include only element nodes if(figureEl.nodeType !== 1) {continue;}linkEl = figureEl.children[0]; // <a> elementsize = linkEl.getAttribute('data-size').split('x');// create slide objectitem = {src: linkEl.getAttribute('href'),w: parseInt(size[0], 10),h: parseInt(size[1], 10)};if(figureEl.children.length > 1) {// <figcaption> contentitem.title = figureEl.children[1].innerHTML; }if(linkEl.children.length > 0) {// <img> thumbnail element, retrieving thumbnail urlitem.msrc = linkEl.children[0].getAttribute('src');} item.el = figureEl; // save link to element for getThumbBoundsFnitems.push(item);}return items;};// find nearest parent elementvar closest = function closest(el, fn) {return el && ( fn(el) ? el : closest(el.parentNode, fn) );};// triggers when user clicks on thumbnailvar onThumbnailsClick = function(e) {e = e || window.event;e.preventDefault ? e.preventDefault() : e.returnValue = false;var eTarget = e.target || e.srcElement;// find root element of slidevar clickedListItem = closest(eTarget, function(el) {return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');});if(!clickedListItem) {return;}// find index of clicked item by looping through all child nodes// alternatively, you may define index via data- attributevar clickedGallery = clickedListItem.parentNode,childNodes = clickedListItem.parentNode.childNodes,numChildNodes = childNodes.length,nodeIndex = 0,index;for (var i = 0; i < numChildNodes; i++) {if(childNodes[i].nodeType !== 1) { continue; }if(childNodes[i] === clickedListItem) {index = nodeIndex;break;}nodeIndex++;}if(index >= 0) {// open PhotoSwipe if valid index foundopenPhotoSwipe( index, clickedGallery );}return false;};// parse picture index and gallery index from URL (#&pid=1&gid=2)var photoswipeParseHash = function() {var hash = window.location.hash.substring(1),params = {};if(hash.length < 5) {return params;}var vars = hash.split('&');for (var i = 0; i < vars.length; i++) {if(!vars[i]) {continue;}var pair = vars[i].split('='); if(pair.length < 2) {continue;} params[pair[0]] = pair[1];}if(params.gid) {params.gid = parseInt(params.gid, 10);}return params;};var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {var pswpElement = document.querySelectorAll('.pswp')[0],gallery,options,items;items = parseThumbnailElements(galleryElement);// define options (if needed)options = {// define gallery index (for URL)galleryUID: galleryElement.getAttribute('data-pswp-uid'),getThumbBoundsFn: function(index) {// See Options -> getThumbBoundsFn section of documentation for more infovar thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnailpageYScroll = window.pageYOffset || document.documentElement.scrollTop,rect = thumbnail.getBoundingClientRect(); return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};}};// PhotoSwipe opened from URLif(fromURL) {if(options.galleryPIDs) {// parse real index when custom PIDs are used // http://photoswipe.com/documentation/faq.html#custom-pid-in-urlfor(var j = 0; j < items.length; j++) {if(items[j].pid == index) {options.index = j;break;}}} else {// in URL indexes start from 1options.index = parseInt(index, 10) - 1;}} else {options.index = parseInt(index, 10);}// exit if index not foundif( isNaN(options.index) ) {return;}if(disableAnimation) {options.showAnimationDuration = 0;}// Pass data to PhotoSwipe and initialize itgallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);gallery.init();};// loop through all gallery elements and bind eventsvar galleryElements = document.querySelectorAll( gallerySelector );for(var i = 0, l = galleryElements.length; i < l; i++) {galleryElements[i].setAttribute('data-pswp-uid', i+1);galleryElements[i].onclick = onThumbnailsClick;}// Parse URL and open gallery if it contains #&pid=3&gid=1var hashData = photoswipeParseHash();if(hashData.pid && hashData.gid) {openPhotoSwipe( hashData.pid , galleryElements[ hashData.gid - 1 ], true, true );} };// execute above function initPhotoSwipeFromDOM('.my-gallery');

    Example on CodePen (focus & history options are disabled due to embed issues):

    提示:你可能需要從CodePen下載例子在本地跑一跑。(Edit on CodePen -> Share -> Export .zip))

    • 如果你需要使用不同的標簽(自定義UI界面),那么你需要編輯功能函數(shù)parseThumbnailElements
    • 如果你對原生js經(jīng)驗不足或不知道如何解析DOM,請參考:quirksmode和documentation on MDN.
    • IE8不支持html5標簽<figure>和<figcation>元素。你需要在<head>標簽中引入html5shiv,如下:
    <!--[if lt IE 9]><script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js"></script> <![endif]-->

    總結(jié)

    以上是生活随笔為你收集整理的PhotoSwipe入门(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 人人爽人人干 | 久久久久夜夜夜精品国产 | 九草视频在线观看 | a级在线观看视频 | 欧美色图12p | 少妇系列在线观看 | 婷婷丁香花五月天 | 精品在线视频一区二区 | 裸体一区二区三区 | 国产亚洲精品一区二区三区 | 欧美五月激情 | 日韩欧美一区二区三区久久婷婷 | av片免费在线播放 | 国产操| 蜜臀99久久精品久久久久小说 | 精品久久网 | 美国少妇在线观看免费 | 国产精品免费av一区二区三区 | 亚洲四虎av | 91传媒理伦片在线观看 | 国产日韩精品suv | 亚洲另类视频 | 老外毛片 | 新版天堂资源中文8在线 | 国内偷拍第一页 | 日本在线www | 免费黄色三级网站 | 日本免费黄视频 | 免费看黄色片的网站 | brazzers欧美一区二区 | 欧美精品videos另类 | a天堂v| 女同在线视频 | 亚洲一区免费在线观看 | 亚洲图片欧美在线看 | 鸥美一级片 | 69毛片| 日日摸日日添日日躁av | 欧美手机在线观看 | 男女深夜福利 | 精品国产亚洲一区二区麻豆 | 另类小说婷婷 | 国产制服av | 毛片网在线观看 | 国产一二三区在线视频 | 亚洲欧洲日本一区二区三区 | 伊人超碰在线 | 麻豆传媒观看 | 国产欧美一区二区精品忘忧草 | 特黄特色大片免费 | 中文人妻av久久人妻18 | 亚洲911精品成人18网站 | 亚洲AV无码精品黑人黑人 | 免费看亚洲 | 美女精品久久 | 国产h视频在线观看 | 人人澡人人看 | 91影音| 日韩免费福利视频 | 久久成人一区二区 | 国产一区二区视频在线观看 | 日本美女全裸 | 精品无码av一区二区三区四区 | 夜夜欢天天干 | 精品国产一区二区三区无码 | 北条麻妃一二三区 | 污动漫网站 | 在线观看亚洲区 | 91网站免费在线观看 | 岛国午夜视频 | 五月婷婷狠狠爱 | xiuxiuavnet| 亚洲自拍偷拍网 | 黑人操亚洲美女 | 99热国产在线观看 | 国产午夜一区二区 | 国产激情综合五月久久 | 欧美综合精品 | 狠狠爱五月婷婷 | 日本视频免费 | 免费av手机在线观看 | 免费看污片网站 | 一本一道久久a久久综合蜜桃 | 欧美aaaaaaa | av在线免费观看一区 | 影音先锋在线国产 | 粉嫩小泬无遮挡久久久久久 | 亚洲国产成人在线观看 | 欧美手机看片 | 色欲一区二区三区精品a片 在线观看黄网站 | 深夜福利免费视频 | 四虎一区二区 | 韩日精品视频 | 亚洲无码国产精品 | 国产黄频 | 免费看一区二区三区 | 三级a做爰全过程 | 国产成人三级在线观看 | 国内精品91 |