jquery offsetParent()源码解读
offsetParent: function() {
return this.map(function() {
var offsetParent = this.offsetParent || docElem; while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" ) && jQuery.css( offsetParent, "position") === "static" ) ) {
offsetParent = offsetParent.offsetParent;
} return offsetParent || docElem;
});
}
代码比较简单,首先它return this.map(function(){}) 所以返回的是一个数组。。。
获取当前元素的offsetParent,如果没有的话就是documentElement
jQuery.nodeName("offsetParent","html")是为了判断当前的offsetParent是否是html,然后jQuery.css( offsetParent, "position") === "static" ) 判断offsetParent是否是static...
但是问题来了,什么时候while为true呢?
( !jQuery.nodeName( offsetParent, "html" )排除了html,但是还有一个body我们不能忽略。 然后我发现。。
console.log(document.getElementById('div').offsetParent);
console.log($('#div').offsetParent());
原生返回body而jQuery返回html
原因,刚在stackoverflow上问了一下,确实,不管jquery返回html或者body都不重要,反正都是说明当前元素没有一个有定位的祖先元素,而jquery返回html的原因,可能就是想做一下标准化吧。。
(PS~stackoverflow相当给力啊,其他的回答明天再看好了,滚回去碎觉。。。)
jquery offsetParent()源码解读的更多相关文章
- jQuery.Callbacks 源码解读二
一.参数标记 /* * once: 确保回调列表仅只fire一次 * unique: 在执行add操作中,确保回调列表中不存在重复的回调 * stopOnFalse: 当执行回调返回值为false,则 ...
- jQuery toggleClass 源码解读
toggleClass: function( value, stateVal ) { var type = typeof value;//值类型 if ( typeof stateVal === &q ...
- jQuery attr() 源码解读
我们知道,$().attr()实质上是内部调用了jQuery.access方法,在调用时jQuery.attr作为回调传入.在通过种种判断(参看jQuery.access()方法)之后,取值和赋值最后 ...
- jQuery position() 源码解读
position的代码比较简单... position: function() { if ( !this[ 0 ] ) { return; } var offsetParent, offset, el ...
- jquery.fileupload源码解读笔记
基础编程风格 新建 test.html 和 test.js和 main.js和 无论哪种顺序 <body> <script src="/Sandeep/js/jquery ...
- jQuery.extend()源码解读
// extend方法为jQuery对象和init对象的prototype扩展方法// 同时具有独立的扩展普通对象的功能jQuery.extend = jQuery.fn.extend = funct ...
- jQuery框架源码解读
1.jQuery 1.9.1 parseJSON: function( data ) { // Attempt to parse using the native JSON parser first ...
- jQuery addClass() 源码解读
addClass: function( value ) { var classes, elem, cur, clazz, j, i = 0, len = this.length, proceed = ...
- jQuery removeAttr() 源码解读
removeAttr比attr的代码要简单很多~~~ removeAttr: function( name ) { return this.each(function() { jQuery.remov ...
随机推荐
- 《数学之美》第15章 矩阵计算和文本处理中两个分类问题——SVD分解的应用
转载请注明原地址:http://www.cnblogs.com/connorzx/p/4170047.html 提出原因 基于余弦定理对文本和词汇的处理需要迭代的次数太多(具体见14章笔记),为了找到 ...
- Chapter 3 Shared Assemblies and Strongly Named Assemblies
As time marches on,MS developers and control developer modify their code:they fix bugs,patch securit ...
- tomcat正常启动,但是java项目没有启动原因
右键项目,选择properties,查看该属性配置的是否正确
- llala js弹出层 颜色渐变
网址:http://bbs.csdn.net/topics/370254842
- Android-Styles and Themes [From API Guide]
This blog was opened 5 months ago and it has 57 posts now,but the poor thing is by now no one has co ...
- ubuntu 16.04 安装 Matlab R2016b后启动出现的问题
(1)报以下错误: License checkout failed.License Manager Error -95MATLAB is unable to connect to the licens ...
- [转]git merge 跟 git merge -no-ff
根据这张图片可以看出 git merge –no-ff 可以保存你之前的分支历史.能够更好的查看 merge历史,以及branch 状态. git merge 则不会显示 feature,只保留单条分 ...
- monkeyRunner
MonkeyRunner工具是使用Jython(使用Java编程语言实现的Python)写出来的,它提供了多个API,通过monkeyrunner API 可以写一个Python的程序来模拟操作控制A ...
- 【网络爬虫】【python】网络爬虫(四):scrapy爬虫框架(架构、win/linux安装、文件结构)
scrapy框架的学习,目前个人觉得比较详尽的资料主要有两个: 1.官方教程文档.scrapy的github wiki: 2.一个很好的scrapy中文文档:http://scrapy-chs.rea ...
- Laravel中的模板引擎Blade
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...