国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > JS > 正文

JS教程:lightbox源碼解析

2024-09-06 12:40:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

lightbox源碼解析

function getpagescroll(){

var yscroll;

if (self.pageyoffset) {
yscroll = self.pageyoffset; //ns
} else if (document.documentelement && document.documentelement.scrolltop){ // explorer 6 strict
yscroll = document.documentelement.scrolltop;
} else if (document.body) {// all other explorers
yscroll = document.body.scrolltop;
}

arraypagescroll = new array('',yscroll)
return arraypagescroll;
}

1. self
打開(kāi)任何一個(gè)網(wǎng)頁(yè),瀏覽器會(huì)首先創(chuàng)建一個(gè)窗口,這個(gè)窗口就是一 個(gè)window 對(duì)象,也是 js 運(yùn)行所依附的全局環(huán)境對(duì)象和全局作用域?qū)ο蟆?br />self 指窗口本身,它返回的對(duì)象 跟window 對(duì)象是一模一樣的。也正因?yàn)槿绱耍瑆indow 對(duì)象的常用方法和函數(shù)都可以用 self 代替 window。

2. 獲得窗口的滾動(dòng)偏移量
* omniweb 4.2-, netfront 3.3- 下無(wú)法獲得.
* safari 和 omniweb 4.5+ 有 bug,但無(wú)影響.

有三種方法獲取滾動(dòng)條的位置。
1. window.pagexoffset/pageyoffset 大多數(shù)瀏覽器,非常可靠
2. document.documentelement.scrollleft/top strict 模式下的 ie6 和其它很少一些瀏覽器
3. document.body.scrollleft/top ie6 和 其它一些瀏覽器

瀏覽器在支持 document.body 或者 document.documentelement 的情況下,如果提供了 scrollleft/top,那么除了 safari 和 omniweb 4.5+ 外, 這些值都是
很可靠的。在 safari and omniweb 4.5+ 中,當(dāng)滾動(dòng)條偏移量為 0 時(shí),會(huì)返回 -8,其它情況下正常。當(dāng)然了,因?yàn)樗鼈兲峁┝苏_的 window.pagexoffset/pageyoffset,
這個(gè) bug 不會(huì)造成什么影響。

function getpagesize(){

//整個(gè)頁(yè)面的大小
var xscroll, yscroll;

if (window.innerheight && window.scrollmaxy) {
xscroll = document.body.scrollwidth;
yscroll = window.innerheight + window.scrollmaxy;
} else if (document.body.scrollheight > document.body.offsetheight){ // all but explorer mac
xscroll = document.body.scrollwidth;
yscroll = document.body.scrollheight;
} else { // explorer mac...would also work in explorer 6 strict, mozilla and safari
xscroll = document.body.offsetwidth;
yscroll = document.body.offsetheight;
}

//可見(jiàn)窗口(view port)的大小
var windowwidth, windowheight;
if (self.innerheight) { // all except explorer
windowwidth = self.innerwidth;
windowheight = self.innerheight;
} else if (document.documentelement && document.documentelement.clientheight) { // explorer 6 strict mode
windowwidth = document.documentelement.clientwidth;
windowheight = document.documentelement.clientheight;
} else if (document.body) { // other explorers
windowwidth = document.body.clientwidth;
windowheight = document.body.clientheight;
}

// for small pages with total height less then height of the viewport
if(yscroll < windowheight){
pageheight = windowheight;
} else {
pageheight = yscroll;
}

// for small pages with total width less then width of the viewport
if(xscroll < windowwidth){
pagewidth = windowwidth;
} else {
pagewidth = xscroll;
}


arraypagesize = new array(pagewidth,pageheight,windowwidth,windowheight)
return arraypagesize;
}

文檔加載完之前是無(wú)法獲取窗口大小值的,而且還要對(duì)不同瀏覽器使用不同的方法。可用參數(shù)如下:
1. window.innerheight/width ie 除外的大多數(shù)瀏覽器
2. document.body.clientheight/width 包括 ie 在內(nèi)的大多數(shù)瀏覽器
3. document.documentelement.clientheight/width 包括 ie 在內(nèi)的大多 dom 瀏覽器

關(guān)于 clientheight/width 會(huì)有點(diǎn)亂,因?yàn)樵诓煌瑸g覽器下,甚至在同一個(gè)瀏覽器下 clientheight/width 都可能不同,要看文檔類型激發(fā)的是瀏覽器的
strict 模式還是 quirks 模式。有時(shí),它們指的是窗口的尺寸,有時(shí)是文檔內(nèi)容的尺寸。下表展示了不同瀏覽器、不同模式中的屬性:

properties and what they relate to
browser window.
innerheight
document.
body.
clientheight
document.
documentelement.
clientheight
opera 9.5+ strict window document window
opera 9.5+ quirks window window document
opera 7-9.2 window window document
opera 6 window window n/a
mozilla strict window document window
mozilla quirks window window document
khtml window document document
safari window document document
icab 3 window document document
icab 2 window window n/a
ie 6+ strict n/a document window
ie 5-7 quirks n/a window 0
ie 4 n/a window n/a
icebrowser window window document
tkhtml hv3 window window document
netscape 4 window n/a n/a

如上所示,好歹還是有個(gè)值是確定的:innerheight,不過(guò) ie 卻不支持這個(gè)屬性。目前,幾乎所有的瀏覽器都支持使用 window.innerheight/width 屬性。

算法邏輯:
1. 如果存在 window.innerheight/width 屬性, 是可以完全信賴的, 使用 window.innerheight/width 就可以了.
2. 否則如果存在 document.documentelement.clientheight/width 屬性且值大于 0,就用 document.documentelement.clientheight/width.
3. 否則就用 document.body.clientheight/width.

此算法在 ie6+ “standards compliant mode” 下,當(dāng)窗口所謂 0px × 0px 大小時(shí),失效。

|||

function showlightbox(objlink)
{
// prep objects
var objoverlay = document.getelementbyid('overlay');
// overlay 為遮罩層
var objlightbox = document.getelementbyid('lightbox');
var objcaption = document.getelementbyid('lightboxcaption');
var objimage = document.getelementbyid('lightboximage');
var objloadingimage = document.getelementbyid('loadingimage');
// 加載圖片
var objlightboxdetails = document.getelementbyid('lightboxdetails');


var arraypagesize = getpagesize();
var arraypagescroll = getpagescroll();

// center loadingimage if it exists
if (objloadingimage) {
// arraypagesize = new array(pagewidth,pageheight,windowwidth,windowheight)
objloadingimage.style.top = (arraypagescroll[1] + ((arraypagesize[3] - 35 - objloadingimage.height) / 2) + 'px');
// top = 滾動(dòng)條位置 + [視口高度 - 35px(瀏覽器狀態(tài)欄高度) - 加載圖片的高度]/2
objloadingimage.style.left = (((arraypagesize[0] - 20 - objloadingimage.width) / 2) + 'px');
objloadingimage.style.display = 'block';
// 設(shè)置加載圖片在頁(yè)面中間,瀏覽器狀態(tài)欄高度約為 35px,滾動(dòng)條欄寬度約為 20px。
}

// set height of overlay to take up whole page and show
// 設(shè)置遮罩層高度
objoverlay.style.height = (arraypagesize[1] + 'px');
objoverlay.style.display = 'block';

// preload image
imgpreload = new image();

imgpreload.onload=function(){
objimage.src = objlink.href;

// center lightbox and make sure that the top and left values are not negative
// and the image placed outside the viewport
var lightboxtop = arraypagescroll[1] + ((arraypagesize[3] - 35 - imgpreload.height) / 2);
var lightboxleft = ((arraypagesize[0] - 20 - imgpreload.width) / 2);

objlightbox.style.top = (lightboxtop < 0) ? "0px" : lightboxtop + "px";
objlightbox.style.left = (lightboxleft < 0) ? "0px" : lightboxleft + "px";


objlightboxdetails.style.width = imgpreload.width + 'px';

if(objlink.getattribute('title')){
objcaption.style.display = 'block';
objcaption.innerhtml = objlink.getattribute('title');
} else {
objcaption.style.display = 'none';
}

// a small pause between the image loading and displaying is required with ie,
// this prevents the previous image displaying for a short burst causing flicker.
if (navigator.appversion.indexof("msie")!=-1){
pause(250);
}

if (objloadingimage) { objloadingimage.style.display = 'none'; }
// 隱藏加載圖

// hide select boxes as they will 'peek' through the image in ie
selects = document.getelementsbytagname("select");
for (i = 0; i != selects.length; i++) {
selects[i].style.visibility = "hidden";
}


objlightbox.style.display = 'block';

// after image is loaded, update the overlay height as the new image might have
// increased the overall page height.
arraypagesize = getpagesize();
objoverlay.style.height = (arraypagesize[1] + 'px');

// check for 'x' keypress
listenkey();

return false;
}

imgpreload.src = objlink.href;

}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 萍乡市| 崇礼县| 奉贤区| 卢湾区| 宁乡县| 景宁| 北票市| 呈贡县| 南京市| 张家港市| 孟津县| 吕梁市| SHOW| 泸溪县| 花垣县| 乌兰察布市| 宁远县| 麻城市| 靖宇县| 武功县| 石台县| 大名县| 安阳市| 类乌齐县| 石城县| 肇庆市| 广昌县| 临桂县| 黑水县| 陇南市| 宁化县| 桐城市| 吉木乃县| 奉化市| 叶城县| 绥德县| 江城| 诸暨市| 乌拉特前旗| 喜德县| 龙川县|