当前位置:编程学习 > JS >>

sina的lightbox效果。

答案:使用时,只需要在A标签处加上rel="lightbox"即可。e.g:
<A href=>
我自己也写了一个,不过涉及两个模块,(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';

上一个:脚本中出现 window.open() access is denied - 拒绝访问 情况一则及分析
下一个:简单的仿Flash文字动画(兼容Mozilla)

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,