JS教程:lightbox源码解析

2016-02-20 00:58 6 1 收藏

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

【 tulaoshi.com - Web开发 】

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

lightbox源码解析

(本文来源于图老师网站,更多请访问https://www.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://www.tulaoshi.com/n/20160220/1632966.html

延伸阅读
标签: Web开发
最近发现DOMDocument对象很重要,还有XMLHTTP也很重要 注意大小写一定不能弄错. 属性: 1Attributes     存储节点的属性列表(只读) 2childNodes     存储节点的子节点列表(只读) 3dataType     返回此节点的数据类型 4Definition     以DTD或XML模式给出...
标签: Web开发
毫无疑问,John Resig 是一个细致且善于思考的人,对于我们通常使用的匿名函数,在他的细究之下,也能挖掘出一些新的东西。通常情况下,当一个函数调用自身时,递归就出现了,对于下面这样的函数调用,我们并不陌生。 1.function yell(n){ 2.       return n 0 ? yell(n-1) + "a" : "hiy&...
标签: Web开发
Screenshots screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('/upload/2007228153419399.gif');}" alt="" src="http://img.warting.com/allimg/...
标签: Web开发
由于 windowonload 事件需要在页面所有内容(包括图片等)加载完后,才执行,但往往我们更希望在 DOM 一加载完就执行脚本。其实在现在大部分主流浏览器上(Firefox 3+,Opera 9+,Safari 3+,Chrome 2+)都提供了这一事件方法: 。 documentaddEventListener init 那对于 IE 我们如何模拟 addDOMLoadEvent 事件呢? Matthias Mille...
标签: Web开发
打包下载 最近在做一个项目,因为页面使用了Cookie,所以要判断用户的浏览器是否支持Cookie,并提示用户如何开启浏览器的Cookie功能。同时,整个项目要配置多语言支持,包括中文、越南语、日语和英语,所以必须有语言配置文件。项目中应用jQuery解析读取XML语言配置文件来实现语言的调度。这是jQuery解析读取XML文件功能的测试源码,现拿出来...

经验教程

298

收藏

12
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部