Fotorama - 深度链接和浏览器历史记录



我喜欢Fotorama使用特定于每张幻灯片的哈希更新URL的事实,但是当我使用浏览器后退按钮时,幻灯片不会更新到以前查看的幻灯片。有没有办法让Fotorama在浏览器历史记录中记录深层链接,以便使用浏览器的后退/前进按钮相应地更新幻灯片放映?谢谢

没有内置的方法可以做到这一点。使用'fotorama:show'事件和fotorama.show()方法编写自己的解决方案:

<script>
  $(function () {
    window.onpopstate = function (e) {
      var hash = location.hash.replace(/^#/, '');
      if (fotorama && hash) {
        fotorama.show(hash);
      }
    };
    var fotorama = $('.fotorama')
        .on('fotorama:show', function (e, fotorama) {
          var hash = fotorama.activeFrame.id || fotorama.activeIndex;
          console.log('fotorama:show', hash);
          if (window.history && location.hash.replace(/^#/, '') != hash) {
            history.pushState(null, null, '#' + hash);
          }
        })
        .fotorama({
          startindex: location.hash.replace(/^#/, '')
        })
        .data('fotorama');
  });
</script>
<!-- Fotorama -->
<div class="fotorama"
     data-auto="false">
  <a href="1.jpg"></a>
  <a href="2.jpg"></a>
  <a href="3.jpg" id="three"></a>
</div>

属性data-auto="false"很重要!

演示:http://jsfiddle.net/artpolikarpov/2Enwa/show/

原始小提琴:http://jsfiddle.net/artpolikarpov/2Enwa/(由于iframe而不起作用)

最新更新