📄 sunrise_library.js
字号:
/*=========================================20081218 添加:SunRise_Dom.js====================================*/
(function(){
var getStyle,
setStyle,
propertyCache = {},
reClassNameCache = {},
document = window.document;
// brower detection
var isOpera = SunRise.ua.opera,
isSafari = SunRise.ua.webkit,
isGecko = SunRise.ua.gecko,
isIE = SunRise.ua.ie;
// regex cache
var patterns = {
HYPHEN: /(-[a-z])/i, // to normalize get/setStyle
ROOT_TAG: /^body|html$/i, // body for quirks mode, html for standards,
OP_SCROLL:/^(?:inline|table-row)$/i
};
var toCamel = function(property) {
if ( !patterns.HYPHEN.test(property) ) {
return property; // no hyphens
}
if (propertyCache[property]) { // already converted
return propertyCache[property];
}
var converted = property;
while( patterns.HYPHEN.exec(converted) ) {
converted = converted.replace(RegExp.$1,
RegExp.$1.substr(1).toUpperCase());
}
propertyCache[property] = converted;
return converted;
//return propertSunRise.replace(/-([a-z])/gi, function(m0, m1) {return m1.toUpperCase()}) // cant use function as 2nd arg yet due to safari bug
};
var getClassRegEx = function(className) {
var re = reClassNameCache[className];
if (!re) {
re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
reClassNameCache[className] = re;
}
return re;
};
// branching at load instead of runtime
if (document.defaultView && document.defaultView.getComputedStyle) { // W3C DOM method
getStyle = function(el, property) {
var value = null;
if (property == 'float') { // fix reserved word
property = 'cssFloat';
}
var computed = el.ownerDocument.defaultView.getComputedStyle(el, '');
if (computed) { // test computed before touching for safari
value = computed[toCamel(property)];
}
return el.style[property] || value;
};
} else if (document.documentElement.currentStyle && isIE) { // IE method
getStyle = function(el, property) {
switch( toCamel(property) ) {
case 'opacity' :// IE opacity uses filter
var val = 100;
try { // will error if no DXImageTransform
val = el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
} catch(e) {
try { // make sure its in the document
val = el.filters('alpha').opacity;
} catch(e) {
}
}
return val / 100;
case 'float': // fix reserved word
property = 'styleFloat'; // fall through
default:
// test currentStyle before touching
var value = el.currentStyle ? el.currentStyle[property] : null;
return ( el.style[property] || value );
}
};
} else { // default to inline only
getStyle = function(el, property) { return el.style[property]; };
}
if (isIE) {
setStyle = function(el, property, val) {
switch (property) {
case 'opacity':
if ( SunRise.lang.isString(el.style.filter) ) { // in case not appended
el.style.filter = 'alpha(opacity=' + val * 100 + ')';
if (!el.currentStyle || !el.currentStyle.hasLayout) {
el.style.zoom = 1; // when no layout or cant tell
}
}
break;
case 'float':
property = 'styleFloat';
default:
el.style[property] = val;
}
};
} else {
setStyle = function(el, property, val) {
if (property == 'float') {
property = 'cssFloat';
}
el.style[property] = val;
};
}
SunRise.Dom = SunRise.Dom||{
get: function(el) {
if (el && (el.nodeType || el.item)) { // Node, or NodeList
return el;
}
if (SunRise.lang.isString(el) || !el) { // id or null
return document.getElementById(el);
}
if (el.length !== undefined) { // array-like
var c = [];
for (var i = 0, len = el.length; i < len; ++i) {
c[c.length] = SunRise.Dom.get(el[i]);
}
return c;
}
return el; // some other object, just pass it back
},
getStyle: function(el, property) {
property = toCamel(property);
var f = function(element) {
return getStyle(element, property);
};
return SunRise.Dom.batch(el, f, SunRise.Dom, true);
},
setStyle: function(el, property, val) {
property = toCamel(property);
var f = function(element) {
setStyle(element, property, val);
};
SunRise.Dom.batch(el, f, SunRise.Dom, true);
},
getXY: function(el) {
var f = function(el) {
// has to be part of document to have pageXY
if ( (el.parentNode === null || el.offsetParent === null ||
this.getStyle(el, 'display') == 'none') && el != el.ownerDocument.body) {
return false;
}
return getXY(el);
};
return SunRise.Dom.batch(el, f, SunRise.Dom, true);
},
getX: function(el) {
var f = function(el) {
return SunRise.Dom.getXY(el)[0];
};
return SunRise.Dom.batch(el, f, SunRise.Dom, true);
},
getY: function(el) {
var f = function(el) {
return SunRise.Dom.getXY(el)[1];
};
return SunRise.Dom.batch(el, f, SunRise.Dom, true);
},
setXY: function(el, pos, noRetry) {
var f = function(el) {
var style_pos = this.getStyle(el, 'position');
if (style_pos == 'static') { // default to relative
this.setStyle(el, 'position', 'relative');
style_pos = 'relative';
}
var pageXY = this.getXY(el);
if (pageXY === false) { // has to be part of doc to have pageXY
return false;
}
var delta = [ // assuming pixels; if not we will have to retry
parseInt( this.getStyle(el, 'left'), 10 ),
parseInt( this.getStyle(el, 'top'), 10 )
];
if ( isNaN(delta[0]) ) {// in case of 'auto'
delta[0] = (style_pos == 'relative') ? 0 : el.offsetLeft;
}
if ( isNaN(delta[1]) ) { // in case of 'auto'
delta[1] = (style_pos == 'relative') ? 0 : el.offsetTop;
}
if (pos[0] !== null) { el.style.left = pos[0] - pageXY[0] + delta[0] + 'px'; }
if (pos[1] !== null) { el.style.top = pos[1] - pageXY[1] + delta[1] + 'px'; }
if (!noRetry) {
var newXY = this.getXY(el);
// if retry is true, try one more time if we miss
if ( (pos[0] !== null && newXY[0] != pos[0]) ||
(pos[1] !== null && newXY[1] != pos[1]) ) {
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(' '));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -