jQuery.fancybox 学习笔记

本文介绍如何使用Fancybox插件实现图片放大预览功能,并解决层叠顺序问题。同时,通过修改模板实现语言本地化,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置堆叠顺序(z-index)

用bootstrap+jquery开发前端,需要给弹出的模态框中的图片,增加单击图片,可以对图片进行放大预览的功能,如果仅仅绑定了fancybox()函数,如:

$("a.fancybox").fancybox();

而没有配置z-index的值,可能出现弹出层在模态框的后面的情况,如:
点击模态框中的图片,预览图出现在了模态框的后面

解决方案:
配置fancybox的覆盖层(overlay)的堆叠顺序(z-index),大约为12500
最终实现代码如下:

$("a.fancybox").fancybox({
  helpers: {
    overlay : {//z-index 仅能在定位元素(position=absolute||fixed)上奏效 ,此时 z-index值可以设置在12500左右
      css : {position: "fixed", "z-index": 12500}
    }
  }
});

效果:
这里写图片描述

语言本地化

fancybox没有提供完整的国际化语言包,但提供了模版自定义方案,我们可以通过修改模版实现语言本地化,即非完整的国际化。
解决方案:

$(".fancybox").fancybox({
  // HTML templates
  tpl: {
    wrap    : '<div class="fancybox-wrap" tabIndex="-1"><div class="fancybox-skin"><div class="fancybox-outer"><div class="fancybox-inner"></div></div></div></div>',
    image   : '<img class="fancybox-image" src="{href}" alt="" />',
    iframe  : '<iframe id="fancybox-frame{rnd}" name="fancybox-frame{rnd}" class="fancybox-iframe" frameborder="0" vspace="0" hspace="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen' + ($.browser.msie ? ' allowtransparency="true"' : '') + '></iframe>',
    error   : '<p class="fancybox-error">请求的内容未找到。<br/>请稍后再试。</p>',
    closeBtn: '<a title="关闭" class="fancybox-item fancybox-close" href="javascript:;"></a>',
    next    : '<a title="下一个" class="fancybox-nav fancybox-next" href="javascript:;"><span></span></a>',
    prev    : '<a title="上一个" class="fancybox-nav fancybox-prev" href="javascript:;"><span></span></a>'
  }
});

效果如下:
这里写图片描述

How to use 1. Setup Include nessesary JS files (FancyBox uses pngFix to fix IE png transparency). --------------------------- /* required */ <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/jquery.fancybox.js"></script> /* optional */ <script type="text/javascript" src="js/jquery.pngFix.js"></script> <script type="text/javascript" src="js/jquery.metadata.js"></script> -------------------------------------------- Include FancyBox CSS file. Dont forget to change image paths. -------------------------------------------- <link rel="stylesheet" href="css/fancybox.css" type="text/css" media="screen"> -------------------------------------------- 2. Add your images Add images and wrap them with a link to the zoomed version -------------------------------------------- <a href="image_big.jpg"><img src="image_small.jpg" alt=""/></a> -------------------------------------------- Optional: Use the title attribute if you want to show a caption Optional: Use the rel attribute to group images 3. Use the plugin Sample examples: -------------------------------------------- $(document).ready(function() { $("p#test1 a").fancybox(); $("p#test2 a").fancybox({ 'hideOnContentClick': true }); $("p#test3 a").fancybox({ 'zoomSpeedIn': 0, 'zoomSpeedOut': 0, 'overlayShow': true }); }); ------------------------------------------- Available options hideOnContentClick Hides FancyBox when cliked on zoomed item (false by default) zoomSpeedIn Speed in miliseconds of the zooming-in animation (no animation if 0) zoomSpeedOut Speed in miliseconds of the zooming-out animation (no animation if 0) frameWidth Default width for iframed and inline content frameHeight Default height for iframed and inline content overlayShow If true, shows the overlay (false by default) overlayOpacity Opacity of overlay (from 0 to 1) itemLoadCallback Custom function to get group items (see example on this page source) Comments
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值