📄 sunrise_dom.js
字号:
this.setXY(el, pos, true);
}
}
};
SunRise.Dom.batch(el, f, SunRise.Dom, true);
},
setX: function(el, x) {
SunRise.Dom.setXY(el, [x, null]);
},
setY: function(el, y) {
SunRise.Dom.setXY(el, [null, y]);
},
getClientWidth: function() {
return SunRise.Dom.getViewportWidth();
},
getClientHeight: function() {
return SunRise.Dom.getViewportHeight();
},
getElementsByClassName: function(className, tag, root, apply) {
tag = tag || '*';
root = (root) ? SunRise.Dom.get(root) : null || document;
if (!root) {
return [];
}
var nodes = [],
elements = root.getElementsByTagName(tag),
re = getClassRegEx(className);
for (var i = 0, len = elements.length; i < len; ++i) {
if ( re.test(elements[i].className) ) {
nodes[nodes.length] = elements[i];
if (apply) {
apply.call(elements[i], elements[i]);
}
}
}
return nodes;
},
hasClass: function(el, className) {
var re = getClassRegEx(className);
var f = function(el) {
return re.test(el.className);
};
return SunRise.Dom.batch(el, f, SunRise.Dom, true);
},
addClass: function(el, className) {
var f = function(el) {
if (this.hasClass(el, className)) {
return false; // already present
}
el.className = SunRise.lang.trim([el.className, className].join(' '));
return true;
};
return SunRise.Dom.batch(el, f, SunRise.Dom, true);
},
removeClass: function(el, className) {
var re = getClassRegEx(className);
var f = function(el) {
if (!className || !this.hasClass(el, className)) {
return false; // not present
}
var c = el.className;
el.className = c.replace(re, ' ');
if ( this.hasClass(el, className) ) { // in case of multiple adjacent
this.removeClass(el, className);
}
el.className = SunRise.lang.trim(el.className); // remove any trailing spaces
return true;
};
return SunRise.Dom.batch(el, f, SunRise.Dom, true);
},
replaceClass: function(el, oldClassName, newClassName) {
if (!newClassName || oldClassName === newClassName) { // avoid infinite loop
return false;
}
var re = getClassRegEx(oldClassName);
var f = function(el) {
if ( !this.hasClass(el, oldClassName) ) {
this.addClass(el, newClassName); // just add it if nothing to replace
return true; // NOTE: return
}
el.className = el.className.replace(re, ' ' + newClassName + ' ');
if ( this.hasClass(el, oldClassName) ) { // in case of multiple adjacent
this.replaceClass(el, oldClassName, newClassName);
}
el.className = SunRise.lang.trim(el.className); // remove any trailing spaces
return true;
};
return SunRise.Dom.batch(el, f, SunRise.Dom, true);
},
batch: function(el, method, o, override) {
el = (el && (el.tagName || el.item)) ? el : SunRise.Dom.get(el); // skip get() when possible
if (!el || !method) {
return false;
}
var scope = (override) ? o : window;
if (el.tagName || el.length === undefined) { // element or not array-like
return method.call(scope, el, o);
}
var collection = [];
for (var i = 0, len = el.length; i < len; ++i) {
collection[collection.length] = method.call(scope, el[i], o);
}
return collection;
},
getDocumentHeight: function() {
var scrollHeight = (document.compatMode != 'CSS1Compat') ? document.body.scrollHeight : document.documentElement.scrollHeight;
var h = Math.max(scrollHeight, SunRise.Dom.getViewportHeight());
return h;
},
getDocumentWidth: function() {
var scrollWidth = (document.compatMode != 'CSS1Compat') ? document.body.scrollWidth : document.documentElement.scrollWidth;
var w = Math.max(scrollWidth, SunRise.Dom.getViewportWidth());
return w;
},
getViewportHeight: function() {
var height = self.innerHeight; // Safari, Opera
var mode = document.compatMode;
if ( (mode || isIE) && !isOpera ) { // IE, Gecko
height = (mode == 'CSS1Compat') ?
document.documentElement.clientHeight : // Standards
document.body.clientHeight; // Quirks
}
return height;
},
getViewportWidth: function() {
var width = self.innerWidth; // Safari
var mode = document.compatMode;
if (mode || isIE) { // IE, Gecko, Opera
width = (mode == 'CSS1Compat') ?
document.documentElement.clientWidth : // Standards
document.body.clientWidth; // Quirks
}
return width;
}
};
var getXY = function() {
if (document.documentElement.getBoundingClientRect) { // IE
return function(el) {
var box = el.getBoundingClientRect();
var rootNode = el.ownerDocument;
return [box.left + SunRise.Dom.getDocumentScrollLeft(rootNode), box.top +
SunRise.Dom.getDocumentScrollTop(rootNode)];
};
} else {
return function(el) { // manually calculate by crawling up offsetParents
var pos = [el.offsetLeft, el.offsetTop];
var parentNode = el.offsetParent;
// safari: subtract body offsets if el is abs (or any offsetParent), unless body is offsetParent
var accountForBody = (isSafari &&
SunRise.Dom.getStyle(el, 'position') == 'absolute' &&
el.offsetParent == el.ownerDocument.body);
if (parentNode != el) {
while (parentNode) {
pos[0] += parentNode.offsetLeft;
pos[1] += parentNode.offsetTop;
if (!accountForBody && isSafari &&
SunRise.Dom.getStyle(parentNode,'position') == 'absolute' ) {
accountForBody = true;
}
parentNode = parentNode.offsetParent;
}
}
if (accountForBody) { //safari doubles in this case
pos[0] -= el.ownerDocument.body.offsetLeft;
pos[1] -= el.ownerDocument.body.offsetTop;
}
parentNode = el.parentNode;
// account for any scrolled ancestors
while ( parentNode.tagName && !patterns.ROOT_TAG.test(parentNode.tagName) )
{
if (parentNode.scrollTop || parentNode.scrollLeft) {
// work around opera inline/table scrollLeft/Top bug (false reports offset as scroll)
if (!patterns.OP_SCROLL.test(SunRise.Dom.getStyle(parentNode, 'display'))) {
if (!isOpera || SunRise.Dom.getStyle(parentNode, 'overflow') !== 'visible') { // opera inline-block misreports when visible
pos[0] -= parentNode.scrollLeft;
pos[1] -= parentNode.scrollTop;
}
}
}
parentNode = parentNode.parentNode;
}
return pos;
};
}
}
})();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -