先前写过一篇文章:【组件篇】ionic3图像手指缩放滑动预览,是原来封装的一个组件的原型,后来用ionic4后,这个组件不兼容,需要改,那时我开始考虑组件的封装不依赖于ionic自身的组件,所以重写了一个。
组件的核心是使用了PhotoSwipe,它是Github上一个热门的开源项目,有近18K的star,可以上官网看效果,其中在手机端的效果如图:
强调一下,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,如下几步。
- npm安装photoswipe依赖:
npm i photoswipe
- 创建Angular组件PhotoSwipeComponent,并在scss文件中导入样式:
@import "~photoswipe/dist/photoswipe.css";
@import "~photoswipe/dist/default-skin/default-skin.css";
- html中添加先前说的dom部分(此处省略)。
- 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);