LoginSignup
1
2

More than 5 years have passed since last update.

RailsでPhotoSwipeを使う

Posted at

PhotoSwipeとは

画像ギャラリーでよく見るUI。サムネが並んだ一覧ページがあって、サムネをクリックすると画像が拡大してモーダルが表示されるというもの。スワイプして次の画像に行ったり前に戻ったりもできる。以下にデモがある。

◆ 公式
http://photoswipe.com/

◆ これをRailsで使う

Gemfileに以下を加えてbundle installでインストールする.

gem 'photoswipe-rails'

app/assets/javascripts/application.jsに以下を加える.

//= require photoswipe

app/assets/stylesheets/application.scssに以下を加える

*= require photoswipe

サムネ一覧ページを作る。


<div class="my-gallery">
  <figure>
    <a href="hogehoge.png" data-size="482x654">
      <img alt="キャプション" src="hogehoge.png"/>
    </a>
    <figcaption>サムネイルの下に表示されるメッセージ</figcaption>
  </figure>

  <figure>
    <a href="fugafuga.png" data-size="482x654">
      <img alt="キャプション" src="fugafuga.png"/>
    </a>
    <figcaption>サムネイルの下に表示されるメッセージ</figcaption>
  </figure>
</div>

<!-- 以下は公式サイトからのコピペ -->
<!-- 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>

<script type="text/javascript">
var initPhotoSwipeFromDOM=function(e){for(var t=function(e){for(var t,n,r,i,o=e.childNodes,a=o.length,l=[],d=0;d<a;d++)1===(t=o[d]).nodeType&&(r=(n=t.children[0]).getAttribute("data-size").split("x"),i={src:n.getAttribute("href"),w:parseInt(r[0],10),h:parseInt(r[1],10)},t.children.length>1&&(i.title=t.children[1].innerHTML),n.children.length>0&&(i.msrc=n.children[0].getAttribute("src")),i.el=t,l.push(i));return l},n=function e(t,n){return t&&(n(t)?t:e(t.parentNode,n))},r=function(e,n,r,i){var o,a,l=document.querySelectorAll(".pswp")[0];if(a=t(n),o={galleryUID:n.getAttribute("data-pswp-uid"),getThumbBoundsFn:function(e){var t=a[e].el.getElementsByTagName("img")[0],n=window.pageYOffset||document.documentElement.scrollTop,r=t.getBoundingClientRect();return{x:r.left,y:r.top+n,w:r.width}}},i)if(o.galleryPIDs){for(var d=0;d<a.length;d++)if(a[d].pid==e){o.index=d;break}}else o.index=parseInt(e,10)-1;else o.index=parseInt(e,10);isNaN(o.index)||(r&&(o.showAnimationDuration=0),new PhotoSwipe(l,PhotoSwipeUI_Default,a,o).init())},i=document.querySelectorAll(e),o=0,a=i.length;o<a;o++)i[o].setAttribute("data-pswp-uid",o+1),i[o].onclick=function(e){(e=e||window.event).preventDefault?e.preventDefault():e.returnValue=!1;var t=e.target||e.srcElement,i=n(t,function(e){return e.tagName&&"FIGURE"===e.tagName.toUpperCase()});if(i){for(var o,a=i.parentNode,l=i.parentNode.childNodes,d=l.length,u=0,p=0;p<d;p++)if(1===l[p].nodeType){if(l[p]===i){o=u;break}u++}return o>=0&&r(o,a),!1}};var l=function(){var e=window.location.hash.substring(1),t={};if(e.length<5)return t;for(var n=e.split("&"),r=0;r<n.length;r++)if(n[r]){var i=n[r].split("=");i.length<2||(t[i[0]]=i[1])}return t.gid&&(t.gid=parseInt(t.gid,10)),t}();l.pid&&l.gid&&r(l.pid,i[l.gid-1],!0,!0)};initPhotoSwipeFromDOM(".my-gallery");
</script>

以上。これでギャラリーページの出来上がり★

◆サンプル

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2