CSS 操做css
经过 JavaScript 操做 CSShtml
<div style="background-color:red; border:1px solid black;" />
div.setAttribute("style'", "background-color:red; border:1px solid black;" );
ele.style.backgroundColor // 对应css 属性 background-color ele.style.cssFloat // 对应 css 属性 float
<div id="ele" style="background:yellow"></div>web
ele.style.cssText; // 返回字符串 "background:yellow"数组
ele.style.cssText = ""; // 清空行内样式浏览器
判断当前浏览器是否支持某个模块app
typeof element.style.attrName === 'string';
// 注意,无论 CSS 属性名的写法带不带连词线,属性上都能反映出该属性是否存在
styleele.style['backgroundColor'] // "" === "string" 返回 true
document.body.style['background-color'] // "" === "string" 返回 true
// 使用的时候,须要把不一样浏览器的 CSS 前缀也考虑进去
ele.style['-webkit-animation'] === 'string'
function isPropertySupported(cssPropStr) { if (cssPropStr in document.body.style){ return true; } var prefixes = ['Moz', 'Webkit', 'O', 'ms', 'Khtml']; var prefProperty = cssPropStr.charAt(0).toUpperCase() + cssPropStr.substr(1); for(var i = 0; i < prefixes.length; i++){ if((prefixes[i] + prefProperty) in document.body.style){ return true; } } return false; } isPropertySupported('background-clip'); // true
document.querySelector('#' + CSS.escape('foo#bar')); // #号,该字符在 CSS 选择器里面有特殊含义 // 不能直接写成 document.querySelector('#foo#bar') // 只能写成 document.querySelector('#foo\\#bar') // 这里必须使用双斜杠的缘由是,单引号字符串自己会转义一次斜杠
// 第一种写法 CSS.supports('transform-origin', '5px'); // true
// 第二种写法 CSS.supports('display: table-cell'); // true
var result = window.getComputedStyle(ele, ':before')["background"];
经过 CSS 向 DOM 添加的元素函数
主要是经过 :before 和 :after 选择器生成,而后用 content 属性指定伪元素的内容flex
// <div id="test">Test content</div> #test:before { content: 'Before '; color: #FF0; } var test = document.getElementById('#test'); // 节点元素的 style 对象没法读写伪元素的样式 var result = window.getComputedStyle(test, ':before').content; var color = window.getComputedStyle(test, ':before').color;
表明网页的一张样式表,包括行内样式表,内部样式表,外部样式表ui
严格来讲,不只包括网页样式表,还包括 XML 文档的样式表spa
document.styleSheets 返回一个相似数组的对象,表明当前页面的全部 StyleSheet 实例(即全部样式表)
// <style id="myStyle"> // </style> var myStyleSheet = document.getElementById('myStyle').sheet; myStyleSheet instanceof StyleSheet; // true
document.styleSheets[0].href
var sheet = document.querySelector('#styleElement').sheet; sheet.insertRule('#block { color: white }', 0); sheet.insertRule('p { color: red }', 1);
浏览器对脚本在样式表里面插入规则有不少限制。因此,这个方法最好放在 try...catch
里使用
@media
规则的生效条件
// HTML 代码以下 // <style id="myStyle"> // @media screen and (min-width: 900px) { // article { display: flex; } // } // </style> var styleSheet = document.getElementById('myStyle').sheet; styleSheet.cssRules[0] instanceof CSSMediaRule // true styleSheet.cssRules[0].media // { // 0: "screen and (min-width: 900px)", // appendMedium: function, // deleteMedium: function, // item: function, // length: 1, // mediaText: "screen and (min-width: 900px)" // } styleSheet.cssRules[0].conditionText // "screen and (min-width: 900px)"
MediaQueryList
对象来存放媒体查询的结果var mql = window.matchMedia("(orientation: portrait)");
var mql = window.matchMedia('(min-width: 400px)'); mql.media // "(min-width: 400px)"
var result = window.matchMedia("(max-width: 700px)"); if (result.matches){ var linkElm = document.createElement('link'); linkElm.setAttribute('rel', 'stylesheet'); linkElm.setAttribute('type', 'text/css'); linkElm.setAttribute('href', 'small.css'); document.head.appendChild(linkElm); }
var mql = window.matchMedia('(max-width: 600px)'); mql.onchange = function(e) { if (e.matches) { /* 视口不超过 600 像素 */ } else { /* 视口超过 600 像素 */ } }
上面代码中,change事件发生后,存在两种可能。
一种是显示宽度从700像素以上变为如下
另外一种是从700像素如下变为以上,
因此在监听函数内部要判断一下当前是哪种状况
var mql = window.matchMedia('(max-width: 600px)'); // 指定监听函数 mql.addListener(mqCallback);
// 撤销监听函数 mql.removeListener(mqCallback); function mqCallback(e) { if (e.matches) { /* 视口不超过 600 像素 */ } else { /* 视口超过 600 像素 */ } }