欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sina的lightbox效果。

程序员文章站 2022-06-19 19:42:50
使用时,只需要在a标签处加上rel="lightbox"即可。e.g:
使用时,只需要在a标签处加上rel="lightbox"即可。e.g:
<a href=http://image2.sina.com.cn/dy/other/74/u1571p1t74d1184f1625dt20060618123137.jpg rel=lightbox><img src=http://image2.sina.com.cn/dy/other/74/u1571p1t74d1184f1625dt20060618123137.jpg height="100" width="100"></a>

我自己也写了一个,不过涉及两个模块,(func.js公用库,和imagesloader.js图片载入类)过阵子一并发上来。
<style type=text/css>
 html #overlay {
 filter: alpha(opacity=50); background-image: url(images/blank.gif); background-color: #000
}
</style>
<script language=javascript type=text/javascript>
<!--//
function addevent(object, type, handler)
{
 if (object.addeventlistener) {
 object.addeventlistener(type, handler, false);
 } else if (object.attachevent) {
 object.attachevent(['on',type].join(''),handler);
 } else {
 object[['on',type].join('')] = handler;
 }
}
function windowsize()
{ // window size object
 this.w = 0;
 this.h = 0;
 return this.update();
}
windowsize.prototype.update = function()
{
 var d = document;
 this.w = 
 (window.innerwidth) ? window.innerwidth
 : (d.documentelement && d.documentelement.clientwidth) ? d.documentelement.clientwidth
 : d.body.clientwidth;
 this.h = 
 (window.innerheight) ? window.innerheight
 : (d.documentelement && d.documentelement.clientheight) ? d.documentelement.clientheight
 : d.body.clientheight;
 return this;
};
function pagesize()
{ // page size object
 this.win = new windowsize();
 this.w = 0;
 this.h = 0;
 return this.update();
}
pagesize.prototype.update = function()
{
 var d = document;
 this.w = 
 (window.innerwidth && window.scrollmaxx) ? window.innerwidth + window.scrollmaxx
 : (d.body.scrollwidth > d.body.offsetwidth) ? d.body.scrollwidth
 : d.body.offsetwidt;
 this.h = 
 (window.innerheight && window.scrollmaxy) ? window.innerheight + window.scrollmaxy
 : (d.body.scrollheight > d.body.offsetheight) ? d.body.scrollheight
 : d.body.offsetheight;
 this.win.update();
 if (this.w < this.win.w) this.w = this.win.w;
 if (this.h < this.win.h) this.h = this.win.h;
 return this;
};
function pagepos()
{ // page position object
 this.x = 0;
 this.y = 0;
 return this.update();
}
pagepos.prototype.update = function()
{
 var d = document;
 this.x =
 (window.pagexoffset) ? window.pagexoffset
 : (d.documentelement && d.documentelement.scrollleft) ? d.documentelement.scrollleft
 : (d.body) ? d.body.scrollleft
 : 0;
 this.y =
 (window.pageyoffset) ? window.pageyoffset
 : (d.documentelement && d.documentelement.scrolltop) ? d.documentelement.scrolltop
 : (d.body) ? d.body.scrolltop
 : 0;
 return this;
};
function useragent()
{ // user agent information
 var ua = navigator.useragent;
 this.iswinie = this.ismacie = false;
 this.isgecko = ua.match(/gecko\//);
 this.issafari = ua.match(/applewebkit/);
 this.isopera = window.opera;
 if (document.all && !this.isgecko && !this.issafari && !this.isopera) {
 this.iswinie = ua.match(/win/);
 this.ismacie = ua.match(/mac/);
 this.isnewie = (ua.match(/msie 5\.5/) || ua.match(/msie 6\.0/));
 }
 return this;
}
// === lightbox ===
function lightbox(option)
{
 var self = this;
 self._imgs = new array();
 self._wrap = null;
 self._box = null;
 self._open = -1;
 self._page = new pagesize();
 self._pos = new pagepos();
 self._ua = new useragent();
 self._expandable = false;
 self._expanded = false;
 self._expand = option.expandimg;
 self._shrink = option.shrinkimg;
 return self._init(option);
}
lightbox.prototype = {
 _init : function(option)
 {
 var self = this;
 var d = document;
 if (!d.getelementsbytagname) return;
 var links = d.getelementsbytagname("a");
 for (var i=0;i<links.length;i++) {
 var anchor = links[i];
 var num = self._imgs.length;
 if (!anchor.getattribute("href")
 || anchor.getattribute("rel") != "lightbox") continue;
 // initialize item
 self._imgs[num] = {src:anchor.getattribute("href"),w:-1,h:-1,title:'',cls:anchor.classname};
 if (anchor.getattribute("title"))
 self._imgs[num].title = anchor.getattribute("title");
 else if (anchor.firstchild && anchor.firstchild.getattribute && anchor.firstchild.getattribute("title"))
 self._imgs[num].title = anchor.firstchild.getattribute("title");
 anchor.onclick = self._genopener(num); // set closure to onclick event
 }
 var body = d.getelementsbytagname("body")[0];
 self._wrap = self._createwrapon(body,option.loadingimg);
 self._box = self._createboxon(body,option);
 return self;
 },
 _genopener : function(num)
 {
 var self = this;
 return function() { self._show(num); return false; }
 },
 _createwrapon : function(obj,imagepath)
 {
 var self = this;
 if (!obj) return null;
 // create wrapper object, translucent background
 var wrap = document.createelement('div');
 wrap.id = 'overlay';
 with (wrap.style) {
 display = 'none';
 position = 'fixed';
 top = '0px';
 left = '0px';
 zindex = '50';
 width = '100%';
 height = '100%';
 }
 if (self._ua.iswinie) wrap.style.position = 'absolute';
 addevent(wrap,"click",function() { self._close(); });
 obj.appendchild(wrap);
 // create loading image, animated image
 var imag = new image;
 imag.onload = function() {
 var spin = document.createelement('img');
 spin.id = 'loadingimage';
 spin.src = imag.src;
 spin.style.position = 'relative';
 self._set_cursor(spin);
 addevent(spin,'click',function() { self._close(); });
 wrap.appendchild(spin);
 imag.onload = function(){};
 };
 if (imagepath != '') imag.src = imagepath;
 return wrap;
 },
 _createboxon : function(obj,option)
 {
 var self = this;
 if (!obj) return null;
 // create lightbox object, frame rectangle
 var box = document.createelement('div');
 box.id = 'lightbox';
 with (box.style) {
 display = 'none';
 position = 'absolute';
 zindex = '60';
 }
 obj.appendchild(box);
 // create image object to display a target image
 var img = document.createelement('img');
 img.id = 'lightboximage';
 self._set_cursor(img);
 addevent(img,'click',function(){ self._close(); });
 addevent(img,'mouseover',function(){ self._show_action(); });
 addevent(img,'mouseout',function(){ self._hide_action(); });
 box.appendchild(img);
 var zoom = document.createelement('img');
 zoom.id = 'actionimage';
 with (zoom.style) {
 display = 'none';
 position = 'absolute';
 top = '15px';
 left = '15px';
 zindex = '70';
 }
 self._set_cursor(zoom);
 zoom.src = self._expand;
 addevent(zoom,'mouseover',function(){ self._show_action(); });
 addevent(zoom,'click', function() { self._zoom(); });
 box.appendchild(zoom);
 addevent(window,'resize',function(){ self._set_size(true); });
 // close button
 if (option.closeimg) {
 var btn = document.createelement('img');
 btn.id = 'closebutton';
 with (btn.style) {
 display = 'inline';
 position = 'absolute';
 right = '10px';
 top = '10px';
 width = '15px';
 height = '15px';
 zindex = '80';
 }
 btn.src = option.closeimg;
 self._set_cursor(btn);
 addevent(btn,'click',function(){ self._close(); });
 box.appendchild(btn);
 }
 // caption text
 var caption = document.createelement('span');
 caption.id = 'lightboxcaption';
 with (caption.style) {
 display = 'none';
 position = 'absolute';
 zindex = '80';
 }
 box.appendchild(caption);
 // create effect image
 if (!option.effectpos) option.effectpos = {x:0,y:0};
 else {
 if (option.effectpos.x == '') option.effectpos.x = 0;
 if (option.effectpos.y == '') option.effectpos.y = 0;
 }
 var effect = new image;
 effect.onload = function() {
 var effectimg = document.createelement('img');
 effectimg.id = 'effectimage';
 effectimg.src = effect.src;
 if (option.effectclass) effectimg.classname = option.effectclass;
 with (effectimg.style) {
 position = 'absolute';
 display = 'none';
 left = [option.effectpos.x,'px'].join('');;
 top = [option.effectpos.y,'px'].join('');
 zindex = '90';
 }
 self._set_cursor(effectimg);
 addevent(effectimg,'click',function() { effectimg.style.display = 'none'; });
 box.appendchild(effectimg);
 };
 if (option.effectimg != '') effect.src = option.effectimg;
 return box;
 },
 _set_photo_size : function()
 {
 var self = this;
 if (self._open == -1) return;
 var imag = self._box.firstchild;
 var targ = { w:self._page.win.w - 30, h:self._page.win.h - 30 };
 var orig = { w:self._imgs[self._open].w, h:self._imgs[self._open].h };
 // shrink image with the same aspect
 var ratio = 1.0;
 if ((orig.w >= targ.w || orig.h >= targ.h) && orig.h && orig.w)

 ratio = ((targ.w / orig.w) < (targ.h / orig.h)) ? targ.w / orig.w : targ.h / orig.h;
 imag.width = math.floor(orig.w * ratio);
 imag.height = math.floor(orig.h * ratio);
 self._expandable = (ratio < 1.0) ? true : false;
 if (self._ua.iswinie) self._box.style.display = "block";
 self._box.style.top = [self._pos.y + (self._page.win.h - imag.height - 30) / 2,'px'].join('');
 self._box.style.left = [((self._page.win.w - imag.width - 30) / 2),'px'].join('');
 self._show_caption(true);
 },
 _set_size : function(onresize)
 {
 var self = this;
 if (self._open == -1) return;
 self._page.update();
 self._pos.update();
 var spin = self._wrap.firstchild;
 if (spin) {
 var top = (self._page.win.h - spin.height) / 2;
 if (self._wrap.style.position == 'absolute') top += self._pos.y;
 spin.style.top = [top,'px'].join('');
 spin.style.left = [(self._page.win.w - spin.width - 30) / 2,'px'].join('');
 }
 if (self._ua.iswinie) {
 self._wrap.style.width = [self._page.win.w,'px'].join('');
 self._wrap.style.height = [self._page.h,'px'].join('');
 }
 if (onresize) self._set_photo_size();
 },
 _show_action : function()
 {
 var self = this;
 if (self._open == -1 || !self._expandable) return;
 var obj = document.getelementbyid('actionimage');
 if (!obj) return;
 obj.src = (self._expanded) ? self._shrink : self._expand;
 obj.style.display = 'inline';
 },
 _hide_action : function()
 {
 var self = this;
 var obj = document.getelementbyid('actionimage');
 if (obj) obj.style.display = 'none';
 },
 _zoom : function()
 {
 var self = this;
 if (self._expanded) {
 self._set_photo_size();
 self._expanded = false;
 } else if (self._open > -1) {
 var imag = self._box.firstchild;
 self._box.style.top = [self._pos.y,'px'].join('');
 self._box.style.left = '0px';
 imag.width = self._imgs[self._open].w;
 imag.height = self._imgs[self._open].h;
 self._show_caption(false);
 self._expanded = true;
 }
 self._show_action();
 },
 _show_caption : function(enable)
 {
 var self = this;
 var caption = document.getelementbyid('lightboxcaption');
 if (!caption) return;
 if (caption.innerhtml.length == 0 || !enable) {
 caption.style.display = 'none';
 } else { // now display caption
 var imag = self._box.firstchild;
 with (caption.style) {
 top = [imag.height + 10,'px'].join(''); // 10 is top margin of lightbox
 left = '0px';
 width = [imag.width + 20,'px'].join(''); // 20 is total side margin of lightbox
 height = '1.2em';
 display = 'block';
 }
 }
 },
 _show : function(num)
 {
 var self = this;
 var imag = new image;
 if (num < 0 || num >= self._imgs.length) return;
 var loading = document.getelementbyid('loadingimage');
 var caption = document.getelementbyid('lightboxcaption');
 var effect = document.getelementbyid('effectimage');
 self._open = num; // set opened image number
 self._set_size(false); // calc and set wrapper size
 self._wrap.style.display = "block";
 if (loading) loading.style.display = 'inline';
 imag.onload = function() {
 if (self._imgs[self._open].w == -1) {
 // store original image width and height
 self._imgs[self._open].w = imag.width;
 self._imgs[self._open].h = imag.height;
 }
 if (effect) {
 effect.style.display = (!effect.classname || self._imgs[self._open].cls == effect.classname)
 ? 'block' : 'none';
 }
 if (caption) caption.innerhtml = self._imgs[self._open].title;
 self._set_photo_size(); // calc and set lightbox size
 self._hide_action();
 self._box.style.display = "block";
 self._box.firstchild.src = imag.src;
 self._box.firstchild.setattribute('title',self._imgs[self._open].title);
 if (loading) loading.style.display = 'none';
 };
 self._expandable = false;
 self._expanded = false;
 imag.src = self._imgs[self._open].src;
 },
 _set_cursor : function(obj)
 {
 var self = this;
 if (self._ua.iswinie && !self._ua.isnewie) return;
 obj.style.cursor = 'pointer';
 },
 _close : function()
 {
 var self = this;
 self._open = -1;
 self._hide_action();
 self._wrap.style.display = "none";
 self._box.style.display = "none";
 }
};
// === main ===
addevent(window,"load",function() {
 var lightbox = new lightbox({
 loadingimg:'http://image2.sina.com.cn/dy/news/2006/0618/images/loading.gif',
 expandimg:'http://image2.sina.com.cn/dy/news/2006/0618/images/expand.gif',
 shrinkimg:'http://image2.sina.com.cn/dy/news/2006/0618/images/shrink.gif',
 effectimg:'images/zzoop.gif',
 effectpos:{x:-40,y:-20},
 effectclass:'images/effectable',
 closeimg:'http://image2.sina.com.cn/dy/news/2006/0618/images/close.gif'
 });
});
//-->
</script>

 <a href=http://image2.sina.com.cn/dy/other/74/u1571p1t74d1184f1625dt20060618123137.jpg rel=lightbox><img src=http://image2.sina.com.cn/dy/other/74/u1571p1t74d1184f1625dt20060618123137.jpg height="100" width="100"></a>
 <a href="http://blog.never-online.net/article.asp?id=83" target="_blank">click here for back点击返回</a>