📄 dom.js
字号:
/*Copyright (c) 2007, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.net/yui/license.txtversion: 2.2.0*//*Copyright (c) 2006, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.net/yui/license.txt*//** * The dom module provides helper methods for manipulating Dom elements. * @module dom * */(function() { var Y = YAHOO.util, // internal shorthand getStyle, // for load time browser branching setStyle, // ditto id_counter = 0, // for use with generateId propertyCache = {}; // for faster hyphen converts // brower detection var ua = navigator.userAgent.toLowerCase(), isOpera = (ua.indexOf('opera') > -1), isSafari = (ua.indexOf('safari') > -1), isGecko = (!isOpera && !isSafari && ua.indexOf('gecko') > -1), isIE = (!isOpera && ua.indexOf('msie') > -1); // regex cache var patterns = { HYPHEN: /(-[a-z])/i }; var toCamel = function(property) { if ( !patterns.HYPHEN.test(property) ) { return property; // no hyphens } if (propertyCache[property]) { // already converted return propertyCache[property]; } while( patterns.HYPHEN.exec(property) ) { property = property.replace(RegExp.$1, RegExp.$1.substr(1).toUpperCase()); } propertyCache[property] = property; return property; //return property.replace(/-([a-z])/gi, function(m0, m1) {return m1.toUpperCase()}) // cant use function as 2nd arg yet due to safari bug }; // branching at load instead of runtime if (document.defaultView && document.defaultView.getComputedStyle) { // W3C DOM method getStyle = function(el, property) { var value = null; var computed = document.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; break; 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 ( typeof el.style.filter == 'string' ) { // 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; default: el.style[property] = val; } }; } else { setStyle = function(el, property, val) { el.style[property] = val; }; } /** * Provides helper methods for DOM elements. * @namespace YAHOO.util * @class Dom */ YAHOO.util.Dom = { /** * Returns an HTMLElement reference. * @method get * @param {String | HTMLElement |Array} el Accepts a string to use as an ID for getting a DOM reference, an actual DOM reference, or an Array of IDs and/or HTMLElements. * @return {HTMLElement | Array} A DOM reference to an HTML element or an array of HTMLElements. */ get: function(el) { if (!el) { return null; } // nothing to work with if (typeof el != 'string' && !(el instanceof Array) ) { // assuming HTMLElement or HTMLCollection, so pass back as is return el; } if (typeof el == 'string') { // ID return document.getElementById(el); } else { // array of ID's and/or elements var collection = []; for (var i = 0, len = el.length; i < len; ++i) { collection[collection.length] = Y.Dom.get(el[i]); } return collection; } return null; // safety, should never happen }, /** * Normalizes currentStyle and ComputedStyle. * @method getStyle * @param {String | HTMLElement |Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements. * @param {String} property The style property whose value is returned. * @return {String | Array} The current value of the style property for the element(s). */ getStyle: function(el, property) { property = toCamel(property); var f = function(element) { return getStyle(element, property); }; return Y.Dom.batch(el, f, Y.Dom, true); }, /** * Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers. * @method setStyle * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements. * @param {String} property The style property to be set. * @param {String} val The value to apply to the given property. */ setStyle: function(el, property, val) { property = toCamel(property); var f = function(element) { setStyle(element, property, val); }; Y.Dom.batch(el, f, Y.Dom, true); }, /** * Gets the current position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false). * @method getXY * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements * @return {Array} The XY position of the element(s) */ 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') { return false; } var parentNode = null; var pos = []; var box; if (el.getBoundingClientRect) { // IE box = el.getBoundingClientRect(); var doc = document; if ( !this.inDocument(el) && parent.document != document) {// might be in a frame, need to get its scroll doc = parent.document; if ( !this.isAncestor(doc.documentElement, el) ) { return false; } } var scrollTop = Math.max(doc.documentElement.scrollTop, doc.body.scrollTop); var scrollLeft = Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft); return [box.left + scrollLeft, box.top + scrollTop]; } else { // safari, opera, & gecko pos = [el.offsetLeft, el.offsetTop]; parentNode = el.offsetParent; if (parentNode != el) { while (parentNode) { pos[0] += parentNode.offsetLeft; pos[1] += parentNode.offsetTop; parentNode = parentNode.offsetParent; } } if (isSafari && this.getStyle(el, 'position') == 'absolute' ) { // safari doubles in some cases pos[0] -= document.body.offsetLeft; pos[1] -= document.body.offsetTop; } } if (el.parentNode) { parentNode = el.parentNode; } else { parentNode = null; } while (parentNode && parentNode.tagName.toUpperCase() != 'BODY' && parentNode.tagName.toUpperCase() != 'HTML') { // account for any scrolled ancestors if (Y.Dom.getStyle(parentNode, 'display') != 'inline') { // work around opera inline scrollLeft/Top bug pos[0] -= parentNode.scrollLeft; pos[1] -= parentNode.scrollTop; } if (parentNode.parentNode) { parentNode = parentNode.parentNode; } else { parentNode = null; } } return pos; }; return Y.Dom.batch(el, f, Y.Dom, true); }, /** * Gets the current X position of an element based on page coordinates. The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false). * @method getX * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements * @return {String | Array} The X position of the element(s) */ getX: function(el) { var f = function(el) { return Y.Dom.getXY(el)[0]; }; return Y.Dom.batch(el, f, Y.Dom, true); }, /** * Gets the current Y position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false). * @method getY * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements * @return {String | Array} The Y position of the element(s) */ getY: function(el) { var f = function(el) { return Y.Dom.getXY(el)[1]; }; return Y.Dom.batch(el, f, Y.Dom, true); }, /** * Set the position of an html element in page coordinates, regardless of how the element is positioned. * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false). * @method setXY * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements * @param {Array} pos Contains X & Y values for new position (coordinates are page-based) * @param {Boolean} noRetry By default we try and set the position a second time if the first fails */ 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');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -