Pikachoose / Fancybox集成 - 灯箱上的导航箭头

时间:2013-04-15 23:11:30

标签: jquery navigation fancybox integration arrows

我正在使用Fancybox与Pikachoose的集成,如下所述: http://www.pikachoose.com/how-to-fancybox/

我正试图让灯箱显示下一个和之前的箭头,但不是在pikachoose舞台上,我遇到了一些麻烦。我尝试在脚本的fancybox部分添加showNavArrows: true的选项,但它不起作用。那么我尝试使用this: {text: {previous: "Previous", next: "Next" }}显示pikachoose上的导航选项 但我一直收到错误,可能我的语法不是在正确的地方? 有人可以帮忙吗?

这是我正在使用的代码:

$(document).ready(function () {
    var a = function (self) {
        self.anchor.fancybox({
            transitionIn: elastic,
            transitionOut: elastic,
            speedIn: 600,
            speedOut: 200,
            overlayShow: false
        });
    };
    $("#pikame").PikaChoose({
        showCaption: false,
        buildFinished: a,
        autoPlay: false,
        transition: [0],
        speed: 500,
        showCaption: false
    });
});

2 个答案:

答案 0 :(得分:7)

http://www.pikachoose.com/how-to-fancybox/中解释的方法存在的问题是您将fancybox绑定到当前的 pikachoose 元素self.anchor

使用这种方法,无法知道哪些图像组属于fancybox图库(您需要多个元素共享相同的rel属性),因为只有一个 pikachoose 图像:显示每个图像动态切换href容器内的src<a>属性(<img>.pika-stage标记)。

作为一种解决方法,您需要构建fancybox元素组 BEFORE 将您的html结构绑定到 pikachoose pikachoose 将修改DOM结构)

1)。所以有这个html结构:

 <div class="pikachoose">
    <ul id="pikame">
        <li>
           <a title="one" href="image01.jpg" id="single_1"><img alt="" src="thumb01.jpg" /></a>
        </li>
        <li>
           <a title="two" href="image02.jpg" id="single_2"><img alt="" src="thumb02.jpg" /></a>
        </li>
        <li>
           <a title="three" href="image03.jpg" id="single_3"><img alt="" src="thumb03.jpg" /></a>
        </li>
    </ul>
 </div>

2)。使用此脚本创建迭代每个锚点的fancybox元素组:

var fancyGallery = []; // fancybox gallery group
$(document).ready(function () {

  $("#pikame").find("a").each(function(i){
    // buidl fancybox gallery group
    fancyGallery[i] = {"href" : this.href, "title" : this.title};
  });

}); // ready

3)。然后将 pikachoose 绑定到同一个选择器#pikame。您可以使用.end()方法在第一个减速选择器上执行此操作而不复制它;)

var fancyGallery = []; // fancybox gallery group
$(document).ready(function () {
  // build fancybox group
  $("#pikame").find("a").each(function(i){
      // buidl fancybox gallery
      fancyGallery[i] = {"href" : this.href, "title" : this.title};
  }).end().PikaChoose({
      autoPlay : false, // optional
      // bind fancybox to big images element after pikachoose is built
      buildFinished: fancy
   }); // PikaChoose
}); // ready

注意我们使用了 pikachoose 选项buildFinished: fancy,当我们点击大图片时,它实际上会触发fancybox图库。

4)。这是功能:

  var fancy = function (self) {
    // bind click event to big image
    self.anchor.on("click", function(e){
      // find index of corresponding thumbnail
      var pikaindex = $("#pikame").find("li.active").index();
      // open fancybox gallery starting from corresponding index
      $.fancybox(fancyGallery,{
        // fancybox options
        "cyclic": true, // optional for fancybox v1.3.4 ONLY, use "loop" for v2.x
        "index": pikaindex // start with the corresponding thumb index
      });
      return false; // prevent default and stop propagation
     }); // on click
  }

注意我们使用click(需要jQuery v1.7 +)将.on()事件绑定到 pikachoose 元素{{1}使用手动方法self.anchor来启动fancybox库。

此解决方法对于fancybox v1.3.4或v2.x同样适用。使用v1.3.4查看 DEMO ,即使使用IE7也可以正常工作;)

答案 1 :(得分:0)

JFK的反应非常好,但还有一些问题需要纠正:

如果在Pikachoose中启用了轮播,则使用此方法计算的索引将为您提供无效的索引,因为pikachoose会通过在li中附加现有ul来操纵DOM:

var pikaindex = $("#pikame").find("li.active").index();

解决方案:

function getCurrentIndex(fancyGallery) {
    var activeLi = $(""#pikame").find("li.active");
    if (activeLi.length != 1) {
        console.error('(getCurrentIndex) - only one image must have an active class set by Pikachoose');
        return -1;
    }

    var activeLiHtml0   = activeLi[0];
    var activeHref      = $(activeLiHtml0).find('img').attr('src');                 // do not look for <a> tags, PikaChoose will remove them
    if (activeHref === null || activeHref.length == 0) {
        console.error('(getCurrentIndex) - can not get href attribute from selected image');
        return -1;
    }

    for (var i=0 ; i<fancyGallery.length ;i++) {
        var obj = fancyGallery[i];
        if (obj.href.indexOf(activeHref) >= 0){
            console.debug('(getCurrentIndex) - found index: ' + i);
            return i;
        }
    }

    console.error('(getCurrentIndex) - this href: <' + activeHref + '> was not found in configured table');
    return -1;
};