ddd-jquery
Version:
de-bowerified, de-amdified, de-gruntified... jQuery.
131 lines • 5.11 kB
JavaScript
var jQuery = require('./core'), strundefined = require('./var/strundefined'), access = require('./core/access'), rnumnonpx = require('./css/var/rnumnonpx'), curCSS = require('./css/curCSS'), addGetHookIf = require('./css/addGetHookIf'), support = require('./css/support'), __unnameddep__ = require('./core/init'), __unnameddep__ = require('./css'), __unnameddep__ = require('./selector');
var docElem = window.document.documentElement;
function getWindow(elem) {
return jQuery.isWindow(elem) ? elem : elem.nodeType === 9 && elem.defaultView;
}
jQuery.offset = {
setOffset: function (elem, options, i) {
var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition, position = jQuery.css(elem, 'position'), curElem = jQuery(elem), props = {};
if (position === 'static') {
elem.style.position = 'relative';
}
curOffset = curElem.offset();
curCSSTop = jQuery.css(elem, 'top');
curCSSLeft = jQuery.css(elem, 'left');
calculatePosition = (position === 'absolute' || position === 'fixed') && (curCSSTop + curCSSLeft).indexOf('auto') > -1;
if (calculatePosition) {
curPosition = curElem.position();
curTop = curPosition.top;
curLeft = curPosition.left;
} else {
curTop = parseFloat(curCSSTop) || 0;
curLeft = parseFloat(curCSSLeft) || 0;
}
if (jQuery.isFunction(options)) {
options = options.call(elem, i, curOffset);
}
if (options.top != null) {
props.top = options.top - curOffset.top + curTop;
}
if (options.left != null) {
props.left = options.left - curOffset.left + curLeft;
}
if ('using' in options) {
options.using.call(elem, props);
} else {
curElem.css(props);
}
}
};
jQuery.fn.extend({
offset: function (options) {
if (arguments.length) {
return options === undefined ? this : this.each(function (i) {
jQuery.offset.setOffset(this, options, i);
});
}
var docElem, win, elem = this[0], box = {
top: 0,
left: 0
}, doc = elem && elem.ownerDocument;
if (!doc) {
return;
}
docElem = doc.documentElement;
if (!jQuery.contains(docElem, elem)) {
return box;
}
if (typeof elem.getBoundingClientRect !== strundefined) {
box = elem.getBoundingClientRect();
}
win = getWindow(doc);
return {
top: box.top + win.pageYOffset - docElem.clientTop,
left: box.left + win.pageXOffset - docElem.clientLeft
};
},
position: function () {
if (!this[0]) {
return;
}
var offsetParent, offset, elem = this[0], parentOffset = {
top: 0,
left: 0
};
if (jQuery.css(elem, 'position') === 'fixed') {
offset = elem.getBoundingClientRect();
} else {
offsetParent = this.offsetParent();
offset = this.offset();
if (!jQuery.nodeName(offsetParent[0], 'html')) {
parentOffset = offsetParent.offset();
}
parentOffset.top += jQuery.css(offsetParent[0], 'borderTopWidth', true);
parentOffset.left += jQuery.css(offsetParent[0], 'borderLeftWidth', true);
}
return {
top: offset.top - parentOffset.top - jQuery.css(elem, 'marginTop', true),
left: offset.left - parentOffset.left - jQuery.css(elem, 'marginLeft', true)
};
},
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;
});
}
});
jQuery.each({
scrollLeft: 'pageXOffset',
scrollTop: 'pageYOffset'
}, function (method, prop) {
var top = 'pageYOffset' === prop;
jQuery.fn[method] = function (val) {
return access(this, function (elem, method, val) {
var win = getWindow(elem);
if (val === undefined) {
return win ? win[prop] : elem[method];
}
if (win) {
win.scrollTo(!top ? val : window.pageXOffset, top ? val : window.pageYOffset);
} else {
elem[method] = val;
}
}, method, val, arguments.length, null);
};
});
jQuery.each([
'top',
'left'
], function (i, prop) {
jQuery.cssHooks[prop] = addGetHookIf(support.pixelPosition, function (elem, computed) {
if (computed) {
computed = curCSS(elem, prop);
return rnumnonpx.test(computed) ? jQuery(elem).position()[prop] + 'px' : computed;
}
});
});
module.exports = jQuery;