📄 style.js
字号:
/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml*/dojo.provide("dojo.style");dojo.require("dojo.graphics.color");dojo.require("dojo.uri.Uri");dojo.require("dojo.lang.common");(function(){ var h = dojo.render.html; var ds = dojo.style; var db = document["body"]||document["documentElement"]; ds.boxSizing = { MARGIN_BOX: "margin-box", BORDER_BOX: "border-box", PADDING_BOX: "padding-box", CONTENT_BOX: "content-box" }; var bs = ds.boxSizing; ds.getBoxSizing = function(node){ if((h.ie)||(h.opera)){ var cm = document["compatMode"]; if((cm == "BackCompat")||(cm == "QuirksMode")){ return bs.BORDER_BOX; }else{ return bs.CONTENT_BOX; } }else{ if(arguments.length == 0){ node = document.documentElement; } var sizing = ds.getStyle(node, "-moz-box-sizing"); if(!sizing){ sizing = ds.getStyle(node, "box-sizing"); } return (sizing ? sizing : bs.CONTENT_BOX); } } /* The following several function use the dimensions shown below +-------------------------+ | margin | | +---------------------+ | | | border | | | | +-----------------+ | | | | | padding | | | | | | +-------------+ | | | | | | | content | | | | | | | +-------------+ | | | | | +-|-------------|-+ | | | +-|-|-------------|-|-+ | +-|-|-|-------------|-|-|-+ | | | | | | | | | | | |<- content ->| | | | | |<------ inner ------>| | |<-------- outer -------->| +-------------------------+ * content-box |m|b|p| |p|b|m| | |<------ offset ----->| | | | |<---- client --->| | | | | | |<-- width -->| | | | * border-box |m|b|p| |p|b|m| | |<------ offset ----->| | | | |<---- client --->| | | | |<------ width ------>| | */ /* Notes: General: - Uncomputable values are returned as NaN. - setOuterWidth/Height return *false* if the outer size could not be computed, otherwise *true*. - (sjmiles) knows no way to find the calculated values for auto-margins. - All returned values are floating point in 'px' units. If a non-zero computed style value is not specified in 'px', NaN is returned. FF: - styles specified as '0' (unitless 0) show computed as '0pt'. IE: - clientWidth/Height are unreliable (0 unless the object has 'layout'). - margins must be specified in px, or 0 (in any unit) for any sizing function to work. Otherwise margins detect as 'auto'. - padding can be empty or, if specified, must be in px, or 0 (in any unit) for any sizing function to work. Safari: - Safari defaults padding values to 'auto'. See the unit tests for examples of (un)computable values in a given browser. */ // FIXME: these work for some elements (e.g. DIV) but not others (e.g. TABLE, TEXTAREA) ds.isBorderBox = function(node){ return (ds.getBoxSizing(node) == bs.BORDER_BOX); } ds.getUnitValue = function(node, cssSelector, autoIsZero){ var s = ds.getComputedStyle(node, cssSelector); if((!s)||((s == 'auto')&&(autoIsZero))){ return { value: 0, units: 'px' }; } if(dojo.lang.isUndefined(s)){return ds.getUnitValue.bad;} // FIXME: is regex inefficient vs. parseInt or some manual test? var match = s.match(/(\-?[\d.]+)([a-z%]*)/i); if (!match){return ds.getUnitValue.bad;} return { value: Number(match[1]), units: match[2].toLowerCase() }; } // FIXME: 'bad' value should be 0? ds.getUnitValue.bad = { value: NaN, units: '' }; ds.getPixelValue = function(node, cssSelector, autoIsZero){ var result = ds.getUnitValue(node, cssSelector, autoIsZero); // FIXME: there is serious debate as to whether or not this is the right solution if(isNaN(result.value)){ return 0; } // FIXME: code exists for converting other units to px (see Dean Edward's IE7) // but there are cross-browser complexities if((result.value)&&(result.units != 'px')){ return NaN; } return result.value; } // FIXME: deprecated ds.getNumericStyle = function() { dojo.deprecated('dojo.(style|html).getNumericStyle', 'in favor of dojo.(style|html).getPixelValue', '0.4'); return ds.getPixelValue.apply(this, arguments); } ds.setPositivePixelValue = function(node, selector, value){ if(isNaN(value)){return false;} node.style[selector] = Math.max(0, value) + 'px'; return true; } ds._sumPixelValues = function(node, selectors, autoIsZero){ var total = 0; for(var x=0; x<selectors.length; x++){ total += ds.getPixelValue(node, selectors[x], autoIsZero); } return total; } ds.isPositionAbsolute = function(node){ return (ds.getComputedStyle(node, 'position') == 'absolute'); } ds.getBorderExtent = function(node, side){ return (ds.getStyle(node, 'border-' + side + '-style') == 'none' ? 0 : ds.getPixelValue(node, 'border-' + side + '-width')); } ds.getMarginWidth = function(node){ return ds._sumPixelValues(node, ["margin-left", "margin-right"], ds.isPositionAbsolute(node)); } ds.getBorderWidth = function(node){ return ds.getBorderExtent(node, 'left') + ds.getBorderExtent(node, 'right'); } ds.getPaddingWidth = function(node){ return ds._sumPixelValues(node, ["padding-left", "padding-right"], true); } ds.getPadBorderWidth = function(node) { return ds.getPaddingWidth(node) + ds.getBorderWidth(node); } ds.getContentBoxWidth = function(node){ node = dojo.byId(node); return node.offsetWidth - ds.getPadBorderWidth(node); } ds.getBorderBoxWidth = function(node){ node = dojo.byId(node); return node.offsetWidth; } ds.getMarginBoxWidth = function(node){ return ds.getInnerWidth(node) + ds.getMarginWidth(node); } ds.setContentBoxWidth = function(node, pxWidth){ node = dojo.byId(node); if (ds.isBorderBox(node)){ pxWidth += ds.getPadBorderWidth(node); } return ds.setPositivePixelValue(node, "width", pxWidth); } ds.setMarginBoxWidth = function(node, pxWidth){ node = dojo.byId(node); if (!ds.isBorderBox(node)){ pxWidth -= ds.getPadBorderWidth(node); } pxWidth -= ds.getMarginWidth(node); return ds.setPositivePixelValue(node, "width", pxWidth); } // FIXME: deprecate and remove ds.getContentWidth = ds.getContentBoxWidth; ds.getInnerWidth = ds.getBorderBoxWidth; ds.getOuterWidth = ds.getMarginBoxWidth; ds.setContentWidth = ds.setContentBoxWidth; ds.setOuterWidth = ds.setMarginBoxWidth; ds.getMarginHeight = function(node){ return ds._sumPixelValues(node, ["margin-top", "margin-bottom"], ds.isPositionAbsolute(node)); } ds.getBorderHeight = function(node){ return ds.getBorderExtent(node, 'top') + ds.getBorderExtent(node, 'bottom'); } ds.getPaddingHeight = function(node){ return ds._sumPixelValues(node, ["padding-top", "padding-bottom"], true); } ds.getPadBorderHeight = function(node) { return ds.getPaddingHeight(node) + ds.getBorderHeight(node); } ds.getContentBoxHeight = function(node){ node = dojo.byId(node); return node.offsetHeight - ds.getPadBorderHeight(node); } ds.getBorderBoxHeight = function(node){ node = dojo.byId(node); return node.offsetHeight; // FIXME: does this work? } ds.getMarginBoxHeight = function(node){ return ds.getInnerHeight(node) + ds.getMarginHeight(node); } ds.setContentBoxHeight = function(node, pxHeight){ node = dojo.byId(node); if (ds.isBorderBox(node)){ pxHeight += ds.getPadBorderHeight(node); } return ds.setPositivePixelValue(node, "height", pxHeight); } ds.setMarginBoxHeight = function(node, pxHeight){ node = dojo.byId(node); if (!ds.isBorderBox(node)){ pxHeight -= ds.getPadBorderHeight(node); } pxHeight -= ds.getMarginHeight(node); return ds.setPositivePixelValue(node, "height", pxHeight); } // FIXME: deprecate and remove ds.getContentHeight = ds.getContentBoxHeight; ds.getInnerHeight = ds.getBorderBoxHeight; ds.getOuterHeight = ds.getMarginBoxHeight; ds.setContentHeight = ds.setContentBoxHeight; ds.setOuterHeight = ds.setMarginBoxHeight; /** * dojo.style.getAbsolutePosition(xyz, true) returns xyz's position relative to the document. * Itells you where you would position a node * inside document.body such that it was on top of xyz. Most people set the flag to true when calling * getAbsolutePosition(). * * dojo.style.getAbsolutePosition(xyz, false) returns xyz's position relative to the viewport. * It returns the position that would be returned * by event.clientX/Y if the mouse were directly over the top/left of this node. */ ds.getAbsolutePosition = ds.abs = function(node, includeScroll){ node = dojo.byId(node); var ret = []; ret.x = ret.y = 0; var st = dojo.html.getScrollTop(); var sl = dojo.html.getScrollLeft(); if(h.ie){ with(node.getBoundingClientRect()){ ret.x = left-2; ret.y = top-2; } }else if(document.getBoxObjectFor){ // mozilla var bo = document.getBoxObjectFor(node); ret.x = bo.x - ds.sumAncestorProperties(node, "scrollLeft"); ret.y = bo.y - ds.sumAncestorProperties(node, "scrollTop"); }else{ if(node["offsetParent"]){ var endNode; // in Safari, if the node is an absolutely positioned child of // the body and the body has a margin the offset of the child // and the body contain the body's margins, so we need to end // at the body if( (h.safari)&& (node.style.getPropertyValue("position") == "absolute")&& (node.parentNode == db)){ endNode = db; }else{ endNode = db.parentNode; } if(node.parentNode != db){ var nd = node; if(window.opera){ nd = db; } ret.x -= ds.sumAncestorProperties(nd, "scrollLeft"); ret.y -= ds.sumAncestorProperties(nd, "scrollTop"); } do{ var n = node["offsetLeft"]; ret.x += isNaN(n) ? 0 : n; var m = node["offsetTop"]; ret.y += isNaN(m) ? 0 : m; node = node.offsetParent; }while((node != endNode)&&(node != null)); }else if(node["x"]&&node["y"]){ ret.x += isNaN(node.x) ? 0 : node.x; ret.y += isNaN(node.y) ? 0 : node.y; } } // account for document scrolling! if(includeScroll){ ret.y += st; ret.x += sl; } ret[0] = ret.x; ret[1] = ret.y; return ret; } ds.sumAncestorProperties = function(node, prop){ node = dojo.byId(node); if(!node){ return 0; } // FIXME: throw an error? var retVal = 0; while(node){ var val = node[prop]; if(val){ retVal += val - 0; if(node==document.body){ break; }// opera and khtml #body & #html has the same values, we only need one value } node = node.parentNode; } return retVal; } ds.getTotalOffset = function(node, type, includeScroll){ return ds.abs(node, includeScroll)[(type == "top") ? "y" : "x"]; } ds.getAbsoluteX = ds.totalOffsetLeft = function(node, includeScroll){ return ds.getTotalOffset(node, "left", includeScroll); } ds.getAbsoluteY = ds.totalOffsetTop = function(node, includeScroll){ return ds.getTotalOffset(node, "top", includeScroll); } ds.styleSheet = null; // FIXME: this is a really basic stub for adding and removing cssRules, but // it assumes that you know the index of the cssRule that you want to add // or remove, making it less than useful. So we need something that can // search for the selector that you you want to remove. ds.insertCssRule = function(selector, declaration, index) { if (!ds.styleSheet) { if (document.createStyleSheet) { // IE ds.styleSheet = document.createStyleSheet(); } else if (document.styleSheets[0]) { // rest // FIXME: should create a new style sheet here // fall back on an exsiting style sheet ds.styleSheet = document.styleSheets[0]; } else { return null; } // fail } if (arguments.length < 3) { // index may == 0 if (ds.styleSheet.cssRules) { // W3 index = ds.styleSheet.cssRules.length; } else if (ds.styleSheet.rules) { // IE index = ds.styleSheet.rules.length; } else { return null; } // fail } if (ds.styleSheet.insertRule) { // W3 var rule = selector + " { " + declaration + " }"; return ds.styleSheet.insertRule(rule, index); } else if (ds.styleSheet.addRule) { // IE return ds.styleSheet.addRule(selector, declaration, index); } else { return null; } // fail } ds.removeCssRule = function(index){ if(!ds.styleSheet){ dojo.debug("no stylesheet defined for removing rules"); return false; } if(h.ie){ if(!index){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -