4

Hi you probably think this is a dumb question but I am trying to get titles added to fancy box 2. I know very little about javascript but have this at the bottom of my html

<script type="text/javascript">
$(document).ready(function() {
    $(".fancybox").fancybox();
});
</script>

AND if I try to add in

$(".fancybox")
.attr('rel', 'gallery')
.fancybox({
    beforeLoad: function() {
        this.title = $(this.element).attr('caption');
}
});

I get all sorts of syntax errors.

Site is: http://bit.ly/Wan5kr

JFK
  • 40,963
  • 31
  • 133
  • 306
Amanda
  • 51
  • 1
  • 1
  • 5

2 Answers2

8

Your current script is :

 $(document).ready(function() {
  $(".fancybox").fancybox({
   helpers : { 
    title : { type : 'inside' }
   }, // helpers
   afterLoad : function() {
    this.title = (this.title ? '' + this.title + '<br />' : '') + 'Image ' + (this.index + 1) + ' of ' + this.group.length;
   } // afterLoad
  }); // fancybox
 }); // ready

... but should be (if you don't want to have "page 1 of 6") :

 $(document).ready(function() {
  $(".fancybox").fancybox({
   helpers : { 
    title : { type : 'inside' }
   } // helpers
  }); // fancybox
 }); // ready

On the other hand, if you prefer to use a caption attribute (because the title shows up as tooltip when you hover the images) the change title by caption like

<a href="images/Gallery/large/wing-back-chair.jpg" caption="Early 1900'....." rel="Sold" class="fancybox"><img width="304" height="350" alt="Winged back chair and ottoman" src="images/Gallery/tn/wing-back-chair.jpg"></a>

and use this script

 $(document).ready(function() {
  $(".fancybox").fancybox({
   helpers : { 
    title : { type : 'inside' }
   }, // helpers
   beforeLoad: function() {
    this.title = $(this.element).attr('caption');
   }
  }); // fancybox
 }); // ready
JFK
  • 40,963
  • 31
  • 133
  • 306
  • Thankyou so much! Your expertise and help is very much appreciated :) – Amanda Nov 03 '12 at 04:42
  • @JFK when i add your solution my thumbnails disappear, what could be the reason? JSFIDDLE: http://jsfiddle.net/uZCC6/5191/ – mikeb Jun 23 '15 at 07:27
0

One (and the simplest) option is to set the tittle attribute of the parent href.

Just like:

<a href="./bigimg/image.jpg" title="My custom title here" id="example">
    <img src="./thumbs/image.jpg" alt="thumbnail of image">
</a>

If you want to place the title inside the lightbox instead of right below it, you need to change the 'titlePostion' attribute. Just like this:

$("a.fancybox").fancybox({
    'titlePosition'  : 'inside'
});

Instead of 'over' you can also place it 'inside' or 'outside'. Outside is the default fancybox behavior. It positions your title right below (outside) of the lightbox. 'Over' places the title inside the lightbox, but on top of your picture with a dark alpha transparent background. 'Inside' places your title inside the lightbox, but below the picture, in the white lightbox border.

You can see the different results on the frontpage of fancybox.net. See the three pictures under: "Different title positions - 'outside', 'inside' and 'over'"

s.meijer
  • 3,403
  • 3
  • 26
  • 23
  • Thanks for your response but I did this and I get one really long title. I would like it to appear within the white frame like lightbox does. – Amanda Nov 01 '12 at 13:00
  • I understand, you need to set the 'titlePosition' attribute then. I edited my earlier answer. Hope this helps. – s.meijer Nov 01 '12 at 13:40
  • @s.meijer : the OP is using fancybox v2.x and `titlePosition` is an option for fancybox v1.3.x which are not compatible. – JFK Nov 01 '12 at 19:12
  • Thanks everyone does this mean I should use lightbox intstead of fancybox? – Amanda Nov 01 '12 at 23:32
  • @Amanda : did you check the links I posted as comments? ... of course you are free to use whatever you know how to use ;) – JFK Nov 02 '12 at 00:46
  • Thanks so much missed the link, just one more little thing please how do i get rid of image 1 of 3. – Amanda Nov 02 '12 at 01:35
  • @Amanda : just remove the whole `afterLoad: function(){...}` callback and only use the `beforeLoad` callback as in your example above. – JFK Nov 02 '12 at 06:22
  • I am sooo sorry but I dont know what you mean. I have this Which lines exactly should I remove? Just this function(){...} – Amanda Nov 02 '12 at 13:10
  • @s.meijer I am also getting errors "document type does not allow
    here. Thanks for sticking with me on this. I really appreciate it.
    – Amanda Nov 02 '12 at 13:19