123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <template>
- <view class="content">
- <div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
- <div class="pswp__bg"></div>
- <div class="pswp__scroll-wrap">
- <div class="pswp__container">
- <div class="pswp__item"></div>
- <div class="pswp__item"></div>
- <div class="pswp__item"></div>
- </div>
- <!-- 预览区域顶部的默认UI,可以修改 -->
- <div class="pswp__ui pswp__ui--hidden">
- <div class="pswp__top-bar">
- <!-- 与图片相关的操作 -->
- <div class="pswp__counter"></div>
- <template v-if="imgs.length>0">
-
- <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
- <div>
- <button class="btn_item" @click.native.prevent="viewPicture"
- v-if="!imgs[idx].picture">查看原图</button>
- </div>
- </template>
- <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>
- <div class="pswp__caption">
- <div class="pswp__caption__center"></div>
- </div>
- </div>
- </div>
- </div>
- </view>
- </template>
- <script>
- import PhotoSwipe from 'photoswipe/dist/photoswipe'
- import PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default'
- import 'photoswipe/dist/photoswipe.css'
- import 'photoswipe/dist/default-skin/default-skin.css'
- var gallery = null
- const promixify = (api) => {
- return (options, ...params) => {
- return new Promise((resolve, reject) => {
- api(Object.assign({}, options, {
- success: resolve,
- fail: resolve
- }), ...params)
- })
- }
- }
- export default {
- name: "photoswipe",
- props: {
- imgs: {
- //图片列表
- type: Array,
- required: true,
- default: () => {
- return [];
- }
- },
- },
- data() {
- return {
- idx:0,
- };
- },
- methods: {
- resImg(){
- if(gallery){
- if(gallery.container.clientHeight>0){
- gallery.updateSize( true );
- }
- }
-
- },
- initImage(e) {
- let that = this
- var pswpElement = document.querySelectorAll('.pswp')[0];
- var items = [];
- const getImageInfo = promixify(uni.getImageInfo)
- Promise.all(
- that.imgs.map(img => getImageInfo({
- src: img.picture ? img.imgUrl : img.acronymUrl
- }))
- ).then((imageInfos) => {
- imageInfos.map((i, k) => {
- i.w = i.width,
- i.h = i.height
- i.src = i.path; //大图
- i.msrc = i.path; //小图
- })
- items = imageInfos
- var options = {
- index: e, // start at first slide
- history: false
- };
- gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
- gallery.init();
- that.idx = gallery.getCurrentIndex()
- gallery.listen('beforeChange', function() {
- that.idx = gallery.getCurrentIndex()
- });
- })
- },
-
- //查看原图
- viewPicture(){
- let that = this
- let idx = gallery.getCurrentIndex()
- that.imgs[idx].picture = true
- let itm = [that.imgs[idx]]
- const getImageInfo = promixify(uni.getImageInfo)
-
- Promise.all(
- itm.map(img => getImageInfo({ src:img.imgUrl }))
- ).then((imageInfos) => {
-
- gallery.items[idx]={
- src:imageInfos[0].path,
- w:imageInfos[0].width,
- h:imageInfos[0].height,
- msrc :imageInfos[0].path,
- };
- gallery.invalidateCurrItems();
- gallery.updateSize( true );
-
- })
-
- },
- }
- }
- </script>
- <style lang="scss">
- .btn_item{
- font-size: 24rpx;
- background: none;
- border: 2rpx solid #FFFFFF;
- color: #FFFFFF;
- position: absolute;
- bottom: 0;
- right: 100rpx;
- height: 60rpx;
- line-height: 60rpx;
- top: 10rpx;
-
- }
- .btn_item::after{
- display: none;
- }
- </style>
|