Title in more than 1 line for FancyBox 2.0.6

纵饮孤独 提交于 2019-12-11 03:55:37

问题


I'm trying to do the exact same thing explained here: Formatting a title for FancyBox

The problem is that I'm using FancyBox version 2.0.6 that it's different from the versione explained in the other topic

/*
 *  Title helper
 */

F.helpers.title = {
    beforeShow: function (opts) {
        var title, text = F.current.title;

        if (text) {
            title = $('<div class="fancybox-title fancybox-title-' + opts.type + '-wrap">' + text + '</div>').appendTo('body');

            if (opts.type === 'float') {
                //This helps for some browsers
                title.width(title.width());

                title.wrapInner('<span class="child"></span>');

                //Increase bottom margin so this title will also fit into viewport
                F.current.margin[2] += Math.abs(parseInt(title.css('margin-bottom'), 10));
            }

            title.appendTo(opts.type === 'over' ? F.inner : (opts.type === 'outside' ? F.wrap : F.skin));
        }
    }
};

Can you help me..? I tried to add the line temp=title.split('|'); if(!temp[1]){temp[1]=""}; after if (text) but it broke everything.. :P


回答1:


With Fancybox v2.x you could set your title in a separated (hidden) <div> just right after the anchor that triggers fancybox like:

<a class="fancybox"  href="images/01.jpg">open image</a>
<div style="display: none;"><!-- my title for fancybox above-->
 <p>Line 1</p>
 <p>line 2 with <a href="#nogo">link</a></p>
</div>

In that way, you can have a more complex title with many lines and html content. Then you could use a much simpler script:

$(document).ready(function() {
 $(".fancybox").fancybox({
  helpers : { 
   title : { type : 'inside' }
  }, // helpers
  /* the following option works fine until version v2.0.5 or below */
  // afterLoad: function(){
  //  this.title = '<div class="myTitle">'+$(this.element).next('div').html()+'</div>';
  // }
  /* the following option should be set for version v2.0.6+ */
      beforeShow: function(){
       this.title = '<div class="myTitle">'+$(this.element).next('div').html()+'</div>';
      }
 }); // fancybox
}); // ready

You can also set separated css declarations for your title:

.myTitle {background-color: #fff; padding: 5px;}
.myTitle p {line-height: 16px; font-size: 12px; padding: 0; margin: 0;}
/* if you want the title stick to the image in fancybox */
.fancybox-title-inside-wrap  {margin-top: 0 !important;}

With this method you don't have to mess with the fancybox js/css files. Also less javascript means less CPU overhead with unnecessary splits, size calculation, wraps, etc.

QUESTION: what if I have more then one image (a gallery)?

Answer: do the same for each image in the html like:

<a class="fancybox" rel="gallery" href="images/01.jpg">open image 1</a>
<div style="display: none;"><!-- my title for fancybox above-->
 <p>Line 1</p>
 <p>line 2 with <a href="#nogo">link</a></p>
</div>
<a class="fancybox" rel="gallery" href="images/02.jpg">open image 2</a>
<div style="display: none;"><!-- my second title -->
 <p>Line 1 for second image title</p>
 <p>line 2 with <a href="#nogo">link</a></p>
 <p>a third line here, why not?</p>
</div>

etc. ... and use the same script.

NOTES: the OP commented:

if I click previous or next button the title disappear.

For fancybox v2.0.6, we need to build the title with the option beforeShow, rather than afterLoad so this line:

  afterLoad: function(){
   this.title = '<div class="myTitle">'+jQuery(this.element).next('div').html()+'</div>';
  }

should be (from v2.0.6):

  beforeShow: function(){
   this.title = '<div class="myTitle">'+jQuery(this.element).next('div').html()+'</div>';
  }

Working demo using v2.0.6



来源:https://stackoverflow.com/questions/10245060/title-in-more-than-1-line-for-fancybox-2-0-6

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