JS教程:lightbox源码解析

葛騰

葛騰

2016-02-20 00:58

清醒时做事,糊涂时读书,大怒时睡觉,无聊时关注图老师为大家准备的精彩内容。下面为大家推荐JS教程:lightbox源码解析,无聊中的都看过来。

lightbox源码解析

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

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
打开任何一个网页,浏览器会首先创建一个窗口,这个窗口就是一 个window 对象,也是 js 运行所依附的全局环境对象和全局作用域对象。
self 指窗口本身,它返回的对象 跟window 对象是一模一样的。也正因为如此,window 对象的常用方法和函数都可以用 self 代替 window。

2. 获得窗口的滚动偏移量
* OmniWeb 4.2-, NetFront 3.3- 下无法获得.
* Safari 和 OmniWeb 4.5+ 有 bug,但无影响.

有三种方法获取滚动条的位置。
1. window.pageXOffset/pageYOffset 大多数浏览器,非常可靠
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+ 中,当滚动条偏移量为 0 时,会返回 -8,其它情况下正常。当然了,因为它们提供了正确的 window.pageXOffset/pageYOffset,
这个 bug 不会造成什么影响。

function getPageSize(){

//整个页面的大小
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;
}

//可见窗口(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;
}

文档加载完之前是无法获取窗口大小值的,而且还要对不同浏览器使用不同的方法。可用参数如下:
1. window.innerHeight/Width IE 除外的大多数浏览器
2. document.body.clientHeight/Width 包括 IE 在内的大多数浏览器
3. document.documentElement.clientHeight/Width 包括 IE 在内的大多 DOM 浏览器

关于 clientHeight/Width 会有点乱,因为在不同浏览器下,甚至在同一个浏览器下 clientHeight/Width 都可能不同,要看文档类型激发的是浏览器的
strict 模式还是 quirks 模式。有时,它们指的是窗口的尺寸,有时是文档内容的尺寸。下表展示了不同浏览器、不同模式中的属性:

Properties and what they relate to Browserwindow.
innerHeightdocument.
body.
clientHeightdocument.
documentElement.
clientHeight Opera 9.5+ strict Opera 9.5+ quirks Opera 7-9.2 Opera 6 Mozilla strict Mozilla quirks KHTML Safari iCab 3 iCab 2 IE 6+ strict IE 5-7 quirks IE 4 ICEbrowser Tkhtml Hv3 Netscape 4
如上所示,好歹还是有个值是确定的:innerHeight,不过 IE 却不支持这个属性。目前,几乎所有的浏览器都支持使用 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 下,当窗口所谓 0px × 0px 大小时,失效。

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

展开更多 50%)
分享

猜你喜欢

JS教程:lightbox源码解析

Web开发
JS教程:lightbox源码解析

Js之软键盘实现(js源码)

Web开发
Js之软键盘实现(js源码)

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

Js之软键盘实现(源码)

Web开发
Js之软键盘实现(源码)

JS评分系统,并提供源码下载

Web开发
JS评分系统,并提供源码下载

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

prototype 源码中文说明之 prototype.js

Web开发
prototype 源码中文说明之 prototype.js

支持IE和firefox的js代码美化加亮源码

Web开发
支持IE和firefox的js代码美化加亮源码

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

Excel2007中输入日期和时间技巧

Excel2007中输入日期和时间技巧

JavaScript教程:Table行定位效果

JavaScript教程:Table行定位效果
下拉加载更多内容 ↓