jQuery仿QQ空间浏览相册

作者:夏雨芭蕉时间:2016-05-10 13:26:08 浏览量:

photoswipe是一款不依赖任何js框架、纯javascript实现的相册效果。它支持移动设备手势操作,如手势触摸滑动、放大和关闭图片,它还支持PC上的键盘操作。本文将以实例演示图集模式和列表模式。

HTML

引入photoswipe相关组件

<link rel="stylesheet" href="css/photoswipe.css">  <link rel="stylesheet" href="css/default-skin/default-skin.css">  <script src="js/photoswipe.min.js"></script>  <script src="js/photoswipe-ui-default.min.js"></script>

我们分析下大图展示结构,提示以下代码中的标签样式:.pswp__bg, .pswp__scroll-wrap, .pswp__container 和 .pswp__item这几个Div不可更改。

<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>            <div class="pswp__ui pswp__ui--hidden">              <div class="pswp__top-bar">                  <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>                  <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

我们在js里定义列表模式图片集合(当然也可以像demo2一样在html部分定义图片集),然后设置API,最后调用photoSwipe插件new PhotoSwipe()。

var openPhotoSwipe = function() {      var pswpElement = document.querySelectorAll('.pswp')[0];      //定义图片集合      var items = [          {              src: 'images/s1.jpg',              w: 800,              h: 1142          },          {              src: 'images/s2.jpg',              w: 800,              h: 1142          }      ];            var options = {          history: false,          focus: false,            showAnimationDuration: 0,          hideAnimationDuration: 0                };            var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);      gallery.init();  };  //点击图集元素时触发调用openPhotoSwipe  document.getElementById('photos').onclick = openPhotoSwipe;


Copyright © 2016 Allister All Rights Reserved