2012-11-27 33 views
1

我想利用標題作爲元素腳本以及縮略圖助手。如果我爲兩者使用相同的類名稱,例如..(.fancybox-thumb),則只有title作爲元素起作用。fancybox標題作爲元素和縮略圖幫手

在此先感謝您的幫助。

標題元素

$(".fancybox-thumb") 
     .attr('rel', 'gallery') 
     .fancybox({ 
      beforeLoad: function() { 
       var el, id = $(this.element).data('title-id'); 

       if (id) { 
        el = $('#' + id); 

        if (el.length) { 
         this.title = el.html(); 
        } 
       } 
      } 
     }); 

縮略圖幫手

$(".fancybox-thumb") 
     .attr('rel', 'gallery') 
     .fancybox({ 
      prevEffect : 'none', 
      nextEffect : 'none', 
      helpers : { 
       title : { 
        type: 'outside' 
       }, 
       thumbs : { 
        width : 60, 
        height : 60 
       } 
      } 

     }); 

如果我使用相同的類名,即雙方..(.fancybox拇指),只有標題爲元素的作品。

<div id="main" class="wrapper clearfix">    
     <a class="fancybox-thumb" rel="fancybox-thumb" href="http://farm7.staticflickr.com/6111/6285920681_67917e8062_b.jpg" title="walk in the park (ewitsoe)"> 
     <img src="http://farm7.staticflickr.com/6111/6285920681_67917e8062_m.jpg" alt="" /> 
    </a> 

    <a class="fancybox-thumb" rel="fancybox-thumb" href="http://farm7.staticflickr.com/6106/6347065961_bb73745e70_b.jpg" data-title-id="title-1"> 
     <img src="http://farm7.staticflickr.com/6106/6347065961_bb73745e70_m.jpg" alt="" /> 
    </a> 
    <div id="title-1" class="hidden"> 
     This is 1st title. <a href="http://google.com" target="blank">Some link</a> 
    </div><!--title-1--> 
</div> <!-- #main --> 

回答

0

集成兩個API選項,如一個腳本里面:

$(".fancybox-thumb").attr('rel', 'gallery').fancybox({ 
    prevEffect: 'none', 
    nextEffect: 'none', 
    helpers: { 
     title: { 
      type: 'outside' 
     }, 
     thumbs: { 
      width: 60, 
      height: 60 
     } 
    }, 
    beforeLoad: function() { 
     var el, id = $(this.element).data('title-id'); 
     if (id) { 
      el = $('#' + id); 
      if (el.length) { 
       this.title = el.html(); 
      } 
     } 
    } 
});​ 

當然,不要忘了加載的fancybox的縮略圖幫手CSS和JS文件如:

<!-- Add fancyBox - thumbnail helper --> 
<link rel="stylesheet" type="text/css" href="/fancybox/source/helpers/jquery.fancybox-thumbs.css?v=2.1.3" /> 
<script type="text/javascript" src="/fancybox/source/helpers/jquery.fancybox-thumbs.js?v=2.1.3"></script> 

編輯:您可以創建不同的畫廊,將畫廊元素分組在不同的父級容器等

<div id="gallery1" class="gallery"> 
    <p>gallery one</p> 
    <a class="fancybox-thumb" href="http://farm8.staticflickr.com/7100/6917703112_d18e3e1b95_b.jpg">one</a> 
    <a class="fancybox-thumb" href="http://farm8.staticflickr.com/7140/7061825385_0ccedf2a8e_b.jpg">two</a> 
</div> 
<div id="gallery2" class="gallery"> 
    <p>gallery two</p> 
    <a class="fancybox-thumb" href="http://farm8.staticflickr.com/7065/7058141285_064170310e_b.jpg">one</a> 
    <a class="fancybox-thumb" href="http://farm6.staticflickr.com/5333/7061356373_1af921fd78_b.jpg">two</a> 
</div> 
<div id="gallery3" class="gallery"> 
    <p>gallery three</p> 
    <a class="fancybox-thumb" href="http://farm8.staticflickr.com/7069/7060779347_fbee5aae15_b.jpg">one</a> 
    <a class="fancybox-thumb" href="http://farm8.staticflickr.com/7053/6918451990_20fa76f338_b.jpg">two</a> 
</div> 

注意,我使用的相同class父容器但不同ID,這將被用於彼此區分畫廊。

然後用一個小的jQuery,創建的fancybox的畫廊,而不需要不同的腳本爲他們每個人:

// loop through each parent container and set rel attribute accordingly 
$("div.gallery").each(function() { 
    var thisID = this.id; 
    $(this).find(".fancybox-thumb").attr("rel", thisID).fancybox({ 
     // API options here 
    }); 
}); // each​​​​​ 

看到一個jsfiddle demo

+0

感謝@JFK,這個偉大工程,爲一個畫廊。我正在使用多個畫廊,所以現在發生的事情是它將所有縮略圖串在一起,就像一個畫廊一樣。我想我可以給每個畫廊它自己的類(.fancybox拇指1)等,這似乎很笨重,但。 你看到一種方法來改變腳本;在這一點上,我假設.attr('rel','gallery')識別多個畫廊? – user1857250

+0

@ user1857250:看到我編輯的答案 – JFK

+1

謝謝@JFK,很棒。我感謝你的時間。 – user1857250

相關問題