zShowBox 图片放大展示jquery版 兼容性


Posted in Javascript onSeptember 24, 2011

zShowBox 图片放大展示jquery版 兼容性
zShowBox.js

/* 
* zShowBox (图片放大展示) 
*/ 
function zShowBox(domChunk) { 
//为每张图片链接加上 class="zshowbox" 
var zcounter = 0; 
$(domChunk + ' a').each(function () { 
var a_href = $(this)[0].href.toLowerCase(); 
var file_type = a_href.substring(a_href.lastIndexOf('.')); 
if (file_type == '.jpeg' || file_type == '.jpg' || file_type == '.png' || file_type == '.gif' || file_type == '.bmp') { $(this).addClass('zshowbox').attr('id', 'zsb-' + zcounter); zcounter++; }; 
}); 
$(domChunk + ' a.zshowbox').click(function () { 
var current = $(this).attr('id').split('zsb-')[1], 
pagesize = zsb_getPageSize(), 
zsb_img_url = $(this).attr('href'), 
css_zsb_bg = 'z-index:9999;overflow:hidden;position:fixed;left:0;top:0;width:100%;height:100%;background:#000 url(' + loadingimg + ') no-repeat center center;', 
css_zsb = 'z-index:99999;position:fixed;left:50%;top:50%;', 
css_zsb_img = 'display:none;border:5px solid #777;border-radius:6px;-moz-border-radius:6px;-webkit-border-radius:6px;box-shadow:1px 1px 5px #333,-1px -1px 5px #333;-moz-box-shadow:1px 1px 5px #333,-1px -1px 5px #333;-webkit-box-shadow:1px 1px 5px #333,-1px -1px 5px #333;', 
css_zsb_p_n = 'display:none;cursor:pointer;position:absolute;top:50%;line-height:80px;margin:-40px 0 0 0;color:#eee;text-shadow:1px 3px 5px #000;font-size:40px;font-family:Arial,Tahoma;'; 
if (typeof document.body.style.maxHeight === "undefined") { //if IE 6 
alert(IE6!太落后了……'); 
return false; 
} else { 
$('body').append('<div id="zsb_bg" style="' + css_zsb_bg + '"></div><div id="zsb" style="' + css_zsb + '"><img id="zsb_img" style="' + css_zsb_img + '" /><p id="zsb_prev" style="left:-30px;' + css_zsb_p_n + '">«</p><p id="zsb_next" style="right:-30px;' + css_zsb_p_n + '">»</p></div>'); 
$('#zsb_bg').fadeTo(600, 0.7); 
zsh_img('#zsb_img', zsb_img_url, pagesize, current, zcounter); 
$('#zsb_prev,#zsb_next').click(function () { 
if ($(this).attr('id') == 'zsb_prev') current--; else current++; 
$(this).parent().prev().css("background-image", 'url(' + loadingimg + ')'); 
$('#zsb').find('img').remove().end().append('<img id="zsb_img" style="' + css_zsb_img + '" />'); 
zsb_img_url = $('#zsb-' + current).attr('href'); 
zsh_img('#zsb_img', zsb_img_url, pagesize, current, zcounter); 
return false; 
}); 
$('#zsb_bg,#zsb_img').click(function () { 
$('#zsb_bg,#zsb_img').unbind('click'); 
$('#zsb_bg,#zsb').fadeOut(400, function () { $(this).remove(); }); 
return false; 
}); 
} 
return false; 
}); 
} 
function zsh_img(img_id, zsb_img_url, pagesize, current, zcounter) { 
$('#zsb_prev,#zsb_next').hide(); 
//动态获取图片的宽度和高度的像素值 
// //IE下,ajax会缓存,导致onreadystatechange函数没有被触发,所以需要加一个随机数 
$(img_id).attr('src', zsb_img_url+ '?t=' + Math.random()).load(function () { 
var img_w,img_h; 
var x = pagesize[0] - 100, y = pagesize[1] - 100; 
//imgReady 图片头数据加载就绪事件 - 更快获取图片尺寸 
imgReady(zsb_img_url, function (){ 
img_w=this.width; img_h=this.height; 
}); 
// img_w = $(this).width(), img_h = $(this).height(); 
if (img_w > x) { 
img_h = img_h * (x / img_w); 
img_w = x; 
if (img_h > y) { 
img_w = img_w * (y / img_h); 
img_h = y; 
} 
} else if (img_h > y) { 
img_w = img_w * (y / img_h); 
img_h = y; 
if (img_w > x) { 
img_h = img_h * (x / img_w); 
img_w = x; 
} 
} 
var marginleft = -(img_w / 2 + 5) + 'px', 
margintop = -(img_h / 2 + 5) + 'px'; 
img_w = img_w + 'px', img_h = img_h + 'px'; 
$(this).css({ "width": img_w, "height": img_h }).fadeIn(600).parent().css({ "margin-left": marginleft, "margin-top": margintop }).prev().css("background-image", "none"); 
if (current > 0){ $('#zsb_prev').show();} 
if (current < zcounter - 1){ $('#zsb_next').show();} 
}); 
} 
function zsb_getPageSize() { 
var de = document.documentElement; 
var w = window.innerWidth || self.innerWidth || (de && de.clientWidth) || document.body.clientWidth; 
var h = window.innerHeight || self.innerHeight || (de && de.clientHeight) || document.body.clientHeight; 
arrayPageSize = [w, h]; 
return arrayPageSize; 
} 
var loadingimg = (function loadingimg() { //获取loading图片url 
var i = 0, got = -1, url, len = document.getElementsByTagName('script').length; 
while (i <= len && got == -1) { 
url = document.getElementsByTagName('script')[i].src; 
got = url.indexOf('/zshowbox.js'); 
i++; 
} 
return url.replace('/zshowbox.js', '/zshowbox-loading.gif'); 
})();

图片头数据加载就绪事件 - 更快获取图片尺寸
/** 
* 图片头数据加载就绪事件 - 更快获取图片尺寸 
* @version 2011.05.27 
* @author TangBin 
* @see http://www.planeart.cn/?p=1121 
* @param {String} 图片路径 
* @param {Function} 尺寸就绪 
* @param {Function} 加载完毕 (可选) 
* @param {Function} 加载错误 (可选) 
* @example imgReady('/upload/201109/20110924011408313.png', function () { 
alert('size ready: width=' + this.width + '; height=' + this.height); 
}); 
*/ 
var imgReady = (function () { 
var list = [], intervalId = null, 
// 用来执行队列 
tick = function () { 
var i = 0; 
for (; i < list.length; i++) { 
list[i].end ? list.splice(i--, 1) : list[i](); 
}; 
!list.length && stop(); 
}, 
// 停止所有定时器队列 
stop = function () { 
clearInterval(intervalId); 
intervalId = null; 
}; 
return function (url, ready, load, error) { 
var onready, width, height, newWidth, newHeight, 
img = new Image(); 
img.src = url; 
// 如果图片被缓存,则直接返回缓存数据 
if (img.complete) { 
ready.call(img); 
load && load.call(img); 
return; 
}; 
width = img.width; 
height = img.height; 
// 加载错误后的事件 
img.onerror = function () { 
error && error.call(img); 
onready.end = true; 
img = img.onload = img.onerror = null; 
}; 
// 图片尺寸就绪 
onready = function () { 
newWidth = img.width; 
newHeight = img.height; 
if (newWidth !== width || newHeight !== height || 
// 如果图片已经在其他地方加载可使用面积检测 
newWidth * newHeight > 1024 
) { 
ready.call(img); 
onready.end = true; 
}; 
}; 
onready(); 
// 完全加载完毕的事件 
img.onload = function () { 
// onload在定时器时间差范围内可能比onready快 
// 这里进行检查并保证onready优先执行 
!onready.end && onready(); 
load && load.call(img); 
// IE gif动画会循环执行onload,置空onload即可 
img = img.onload = img.onerror = null; 
}; 
// 加入队列中定期执行 
if (!onready.end) { 
list.push(onready); 
// 无论何时只允许出现一个定时器,减少浏览器性能损耗 
if (intervalId === null) intervalId = setInterval(tick, 40); 
}; 
}; 
})();

调用:
zShowBox('.gridlist');

附加 loging

zShowBox 图片放大展示jquery版 兼容性
打包下载地址

Javascript 相关文章推荐
基于Jquery的跨域传输数据(JSONP)
Mar 10 Javascript
Jquery判断IE6等浏览器的代码
Apr 05 Javascript
js鼠标滑过弹出层的定位IE6bug解决办法
Dec 26 Javascript
Jquery取得iframe下内容的方法
Nov 18 Javascript
xmlhttp缓存清除的2种解决方法
Dec 13 Javascript
jquery超简单实现手风琴效果的方法
Jun 05 Javascript
js实现的二级横向菜单条实例
Aug 22 Javascript
使用jQuery.Qrcode插件在客户端动态生成二维码并添加自定义Logo
Sep 01 Javascript
Ubuntu 16.04 64位中搭建Node.js开发环境教程
Oct 19 Javascript
微信小程序 利用css实现遮罩效果实例详解
Jan 21 Javascript
微信小程序实现联动选择器
Feb 15 Javascript
vue 中 beforeRouteEnter 死循环的问题
Apr 23 Javascript
也说JavaScript中String类的replace函数
Sep 22 #Javascript
javascript笔记 String类replace函数的一些事
Sep 22 #Javascript
Prototype的Class.create函数解析
Sep 22 #Javascript
Javascript中的this绑定介绍
Sep 22 #Javascript
StringTemplate遇见jQuery冲突的解决方法
Sep 22 #Javascript
jquery实现的让超出显示范围外的导航自动固定屏幕最顶上
Sep 22 #Javascript
javascript代码编写需要注意的7个小细节小结
Sep 21 #Javascript
You might like
DC四月将推出百页特刊漫画 纪念小丑诞生80周年
2020/04/09 欧美动漫
第四节 构造函数和析构函数 [4]
2006/10/09 PHP
PHP实现通用alert函数的方法
2015/03/11 PHP
PHP获取真实客户端的真实IP
2017/03/07 PHP
发布BlueShow v1.0 图片浏览器(类似lightbox)blueshow.js 打包下载
2007/07/21 Javascript
基于jquery的一个简单的脚本验证插件
2010/04/05 Javascript
获取服务器传来的数据 用JS去空格的正则表达式
2012/03/26 Javascript
JavaScrip实现PHP print_r的数功能(三种方法)
2013/11/12 Javascript
JS中with的替代方法与String中的正则方法详解
2016/12/23 Javascript
JavaScript利用闭包实现模块化
2017/01/13 Javascript
uploader秒传图片到服务器完整代码
2017/04/22 Javascript
jQuery操作css样式
2017/05/15 jQuery
js图片轮播插件的封装
2017/07/21 Javascript
详解Vue.js Mixins 混入使用
2017/09/15 Javascript
Vue 第三方字体图标引入 Font Awesome的方法
2018/09/28 Javascript
基于 jQuery 实现键盘事件监听控件
2019/04/04 jQuery
用Vue.js方法创建模板并使用多个模板合成
2019/06/28 Javascript
小程序实现短信登录倒计时
2019/07/12 Javascript
使用easyui从servlet传递json数据到前端页面的两种方法
2019/09/05 Javascript
jQuery 函数实例分析【函数声明、函数表达式、匿名函数等】
2020/05/19 jQuery
[02:49]2018DOTA2亚洲邀请赛主赛事决赛日战况回顾 Mineski鏖战5局夺得辉耀
2018/04/10 DOTA
python基于xml parse实现解析cdatasection数据
2014/09/30 Python
Python教程之全局变量用法
2016/06/27 Python
Python用UUID库生成唯一ID的方法示例
2016/12/15 Python
Python使用爬虫爬取静态网页图片的方法详解
2018/06/05 Python
python 设置xlabel,ylabel 坐标轴字体大小,字体类型
2019/07/23 Python
浅谈ROC曲线的最佳阈值如何选取
2020/02/28 Python
python str字符串转uuid实例
2020/03/03 Python
Python如何进行时间处理
2020/08/06 Python
python 基于selectors库实现文件上传与下载
2020/12/31 Python
python自动化发送邮件实例讲解
2021/01/04 Python
SmartBuyGlasses台湾:名牌眼镜,名牌太阳眼镜及隐形眼镜
2017/01/04 全球购物
以工厂直接定价的传奇性能:Ben Hogan Golf
2019/01/04 全球购物
酒店周年庆活动方案
2014/08/21 职场文书
民警群众路线教育实践活动对照检查材料
2014/10/04 职场文书
公司2015年终工作总结
2015/05/26 职场文书