How to not show the first image when you open the gallery - Fancybox 2

余生颓废 提交于 2019-12-01 10:43:53

If the first image is not going to be included in the fancybox gallery, then it doesn't need to have the fancybox class since it will be used to fire the fancybox gallery only.

You could use a different class (e.g. fancyboxLauncher)

<a class="fancyboxLauncher" href="http://fancyapps.com/fancybox/demo/1_b.jpg" title="one"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt="" /></a>

... and bind a click event to it like :

$(".fancyboxLauncher").on("click", function(){
    $(".fancybox").eq(0).trigger("click");
    return false;
});

... notice that we used the method .eq(0) to fire the gallery from the first item, otherwise it will start from the last. Also notice that .on() requires jQuery v1.7+

You still need this code for the fancybox gallery

$(".fancybox")
    .attr('rel', 'gallery')
    .fancybox({
    padding: 0
});

Additionally, if the images for the fancybox gallery will be hidden, they actually don't need to have a thumbnail (img tag) otherwise you will be just adding an unnecessary overhead to your page load so you could do

<div class="hidden">
   <a class="fancybox" href="http://fancyapps.com/fancybox/demo/2_b.jpg"></a>
   <a class="fancybox" href="http://fancyapps.com/fancybox/demo/3_b.jpg"></a>
</div>

Check JSFIDDLE

In case someone is looking for an alternate answer, you could do something like:

<a class="fancybox" rel="group1" href="images/second-image.jpg">
  <img src="images/first-image.jpg" />
</a>
<!-- clicking on the above image will open second-image.jpg in the fancybox,
     though first-image.jpg is shown as the thumbnail. -->
<a class="fancybox" rel="group1" href="images/third-image.jpg"></a>
<a class="fancybox" rel="group1" href="images/fourth-image.jpg"></a>
<a class="fancybox" rel="group1" href="images/and-so-on.jpg"></a>

This way you don't need to write extra CSS (to hide) nor jquery (to trigger)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!