Angular+PhotoSwipe實現圖片預覽組件

先前寫過一篇文章:【組件篇】ionic3圖像手指縮放滑動預覽,是原來封裝的一個組件的原型,后來用ionic4后,這個組件不兼容,需要改,那時我開始考慮組件的封裝不依賴于ionic自身的組件,所以重寫了一個。

組件的核心是使用了PhotoSwipe,它是Github上一個熱門的開源項目,有近18K的star,可以上官網看效果,其中在手機端的效果如圖:

image.png

強調一下,PhotoSwipe響應式的,并支持手勢操作!

基于Angular封裝的版本,別人不是沒有做過,只是我覺得重新寫一個也很容易,便造了輪子。

封裝前,我們先分析下原生js方式使用:三步走。

  • 第一步,它依賴這些文件,
<!-- 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> 
  • 第二步,html中添加下面內容:
<!-- 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 https://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>

注意:別看內容那么多就嚇到了,其實里面就多了一些按鈕,你可以按需移除。

  • 第三步,最后js初始化使用:
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();

整個操作邏輯很簡單,代碼容易看得懂,所以我不過多解釋了,然后我們改造成Angular,如下幾步。

  1. npm安裝photoswipe依賴:
npm i photoswipe
  1. 創建Angular組件PhotoSwipeComponent,并在scss文件中導入樣式:
@import "~photoswipe/dist/photoswipe.css";
@import "~photoswipe/dist/default-skin/default-skin.css";
  1. html中添加先前說的dom部分(此處省略)。
  2. ts文件封裝方法:
import { Component, OnInit, Input, ChangeDetectorRef } from '@angular/core';
import * as PhotoSwipe from 'PhotoSwipe';
import * as PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default';

@Component({
  selector: 'photo-swipe',
  templateUrl: './photo-swipe.component.html',
  styleUrls: ['./photo-swipe.component.scss']
})
export class PhotoSwipeComponent implements OnInit {

  constructor(private cd: ChangeDetectorRef) { }
  ngOnInit() {
  }
  open(images: PhotoSwipe.Item[], options?: any) {
    this.cd.detectChanges();
    const pswpEle: any = document.querySelectorAll('.pswp')[0];
    // define options (if needed)
    if (!options) {
      options = {
        // optionName: 'option value' for example:
        index: 0 // start at first slide
      };
    }
    // Initializes and opens PhotoSwipe
    const gallery = new PhotoSwipe(pswpEle, PhotoSwipeUI_Default, images, options);
    gallery.init();
  }
}

這樣組件就完成了,使用時這樣應用:

在任何文件任何地方放置這樣一個組件:

<photo-swipe></photo-swipe>

然后如下調用:

@ViewChild(PhotoSwipeComponent)
photoSwipe: PhotoSwipeComponent;
    ……
this.photoSwipe.open(imgList);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容