jQuery-fancybox图片预览

本文介绍了一个修改后的jQuery-fancybox版本,该版本支持Base64编码的图片预览。文章提供了详细的使用说明和代码示例,并指出该版本暂不支持IE8。

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

之前写的内容有问题(当时是在项目中直接跑的,未拿出来单独测试),今天做了修正。对之前给同胞们造成的麻烦十分抱歉……

--以下是正文------------------------------------------------------------

原生的jQuery-fancybox图片预览不支持Base64编码,这里给出经过改造后支持Base64编码的版本,但该版本的Base64方式暂时无法对IE8支持(IE8会出现图片显示不全的问题),具体效果说明:

  1. 弹框的宽高为自动,即根据浏览器显示区域的宽高自动调整,图片宽高比为原图宽高比(可为要预览的图片添加【img-width-**px】或【img-height-**px】或【img-proportion-**%】的class以指定图片宽、高或图片显示比例);
  2. 支持图片左右切换,支持图片旋转,不支持放大缩小;

静态资源文件链接:JQuery-fancybox(支持Base64)静态资源

使用说明:

  • 1、引入jquery.fancybox.js,以及css资源(css资源需放在一个目录下):
  • 2、设置jquery.fancybox.js中的【basePath】、【cssBasePath】:
  • 3、页面初始化时进行fancybox配置:
window.onload=function(){
    // 图片预览配置
    $(".fancybox").fancybox({
            helpers : {title : {type : 'over'}},
            openEffect	: 'none',
            closeEffect	: 'none',
            errorMessage : {image:'该图片无法打开!',content:'该图片无法打开!'},
            // 去除点击旋转、左右图片切换
            tpl: {
                  // 如需支持点击旋转,注掉下面这一行
                  wrap : '<div class="fancybox-wrap" tabIndex="-1" style="position:relative; z-index:99999;"><div class="fancybox-skin"><div class="fancybox-outer"><div class="fancybox-inner"></div></div></div></div>'
                  // 如需支持左右图片切换,注掉下面这两行
                  ,next: ''
                  ,prev: ''
            }
    });
};
  • 4、给要预览的图片添加属性【href】,值与其属性【src】一样,若为Base64编码,需有前缀满足正则【data:image\/.*,】(如:data:image/jpg;base64,):
<img data-fancybox-group="groupName" src="imgAdressOrBase64Code" class="fancybox img-proportion-50%" href="imgAdressOrBase64Code" style="width:100px;height:100px"/>

 

扩展:

  • 1、对全局图片添加fancybox图片预览,若不需进行图片预览的图片需添加class【not-fancybox】:
window.onload=function(){
    $("img").addClass("fancybox");
    // 若图片有not-fancybox的class,则不使用插件
    $("img.not-fancybox").removeClass("fancybox");
    $("img").each(function(){
        $(this).attr("href",$(this).prop("src"));
    });
};

 

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值