2012-09-13 53 views
1

How do I open fancybox via manual call for a gallery in the html not via the jquery options?的fancybox - 如何通過人工呼叫

這回答手動打開畫廊的問題,使用的fancybox畫廊打開特定的圖像。

但是,如果我想在圖庫中打開特定圖像怎麼辦?它可以是圖庫列表中的任何圖像。有沒有辦法做到這一點?

我已經試過這

$("#launcher").on("click", function(){ 
$(".fancybox.default").eq(0).trigger("click"); 
}); 

,我增加一個額外的類「默認」因爲我要顯示的圖像。

但是當我運行它,它顯示圖像,但沒有畫廊的下一個/上一級按鈕。

+1

參考此鏈接..它會幫助你 http://stackoverflow.com/questions/10835849/how-do-i-open-fancybox-via-manual-call-for-a-gallery- in-the-html-not-via-the-jqu – Sathish

+0

@Sathish我已經在我的問題的頂部發布了該鏈接 – arvinsim

回答

1

你必須模擬你想要關注的元素的點擊。

JQuery允許您使用.click函數(link)執行此操作。

我舉了一個例子讓它關注組的元素。 第一種方法使用相同的選擇器,的fancybox用於創建組

<script> 
    $(document).ready(function(){ 
     //get the group of your fancy elements 
     var fancygroup = $("a[rel=example_group]"); 

     // get the element you want to focus (e.g the first element) 
     var fancyelem = $(fancygroup.get(1)); 

     // simulate a click 
     fancyelem.click(); 
    }); 
</script> 

Alternitvly可以指定一個ID要聚焦和模擬點擊它

<p> 
    Image gallery (ps, try using mouse scroll wheel)<br /> 

    <a rel="example_group" href="./example/9_b.jpg" title="Lorem ipsum dolor sit amet"><img alt="" src="./example/9_s.jpg" /></a> 

    <a rel="example_group" href="./example/10_b.jpg" title=""><img alt="" src="./example/10_s.jpg" /></a> 

    <a id="initthis" rel="example_group" href="./example/11_b.jpg" title=""><img alt="" src="./example/11_s.jpg" /></a> 

    <a rel="example_group" href="./example/12_b.jpg" title=""><img class="last" alt="" src="./example/12_s.jpg" /></a> 
</p> 

(ID的元素分配給一個THRID元件

,然後直接選擇該元素:

<script> 
    $(document).ready(function(){ 
     $('#initthis').click(); 
    }); 
</script> 

BR, 斯蒂芬

0

$( 「#ID」)。單擊(函數(){ $( 「一個[相對= gallerimages]」)。的fancybox({ })。觸發器( '點擊') ; });