獲取元素css樣式的方法

獲取元素css樣式的方法

  • 元素.style.屬性
    • 只能獲取行內樣式
  • getComputedStyle (元素).屬性名(IE不兼容)
    • getComputedStyle是window下一個方法;把對應的元素傳給這個方法
    • 他是window上的一個屬性
    • 獲取瀏覽器計算的樣式
    • 如果是複合屬性名,把-去掉,換成駝峯命名;
    • `console.log(getComputedStyle(元素).屬性名)``
    • `console.log(getComputedStyle(box).width)``
    • `console.log(getComputedStyle(box)[‘width’])``
  • currentStyle(只有IE兼容)
    • 元素.currentStyle.屬性名
    • IE8及以下,用這個方法獲取元素css中設置的樣式;
    • 他是元素身上的屬性
function getCss(curEle, attr){ // curEle元素  attr屬性名
  if('getComputedStyle' in window){
    return getComputedStyle(curEle)[attr]
  }
  else {
    return curEle.currentStyle[attr]
  }
}
---------------------------------------------
  function getCss(curEle, attr) {
  try {
    return getComputedStyle(curEle)[attr]
  }catch(e){
    return curEle.currentStyle[attr]
  }
}
  • 例子
<body>
    <div id="box" style="width:800px"></div>
    <script>
        let box = document.getElementById('box');
        // 獲取元素的css樣式

        // 1、元素.style.width 這是獲取的行內樣式
        // console.log(box.style.width)  '' // 獲取不到
        // 2、getComputedStyle 
            // 1、他是window上的一個屬性
            // 2、getComputedStyle(元素).屬性名
    //     console.log('getComputedStyle' in window) // true
    //    console.log(getComputedStyle(box).width)  // '100px'
    //    console.log(getComputedStyle(box)['width']) // '100px'
    //    console.log(getComputedStyle(box)['backgroundColor']) //'rbg(255,0,0)'
        // 3、 currentStyle
            // 1、這個屬性只在IE下存在
            // 2、元素.currentStyle.屬性名
        // console.log(box.currentStyle.width);
        console.log('getComputedStyle' in window);

        /* 
        封裝一個獲取scc樣式的方法,
        */
        function getCss(curEle, attr){ // curEle元素  attr屬性名
          	let val = null;
            if('getComputedStyle' in window){
                val = getComputedStyle(curEle)[attr]
            }
            else {
                val = curEle.currentStyle[attr]
            }
          	let reg = /^(width|height|top|bottom|left|right|padding|margin|fontSize)&/
            if (reg.test(attr)) {
                val = parseFloat(val)
            }
            return val
        }
        console.log(getCss(box, 'width'))
    </script>
</body>

封裝獲取和設置css的方法

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        #box {
            width: 100px;
            height: 100px;
            background: red;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        /* 
        封裝
        */
        function getCss(curEle, attr) {
            let val = null
            if ('getComputedStyle' in window) {
                val = getComputedStyle(curEle)[attr]
            } else {
                val = curEle.currentStyle[attr]
            }
            let reg = /^(width|height|top|bottom|left|right|padding|margin|fontSize)$/
            if (reg.test(attr)) {
                val = parseFloat(val)
            }
            return val
        }

        function setCss(curEle, attr, val) {
            let reg = /^(width|height|top|bottom|left|right|padding|margin|fontSize)$/
            if (reg.test(attr)) {
                if (typeof val === 'number') {
                    val = val + 'px'
                }
            }
            curEle.style[attr] = val
        }
        // setCss(box, 'width', 200)
        // setCss(box, 'height', '200px')
        // setCss(box, 'backgroundColor', 'green')
        
        function setGroupCss(curEle, obj) {
            for (key in obj) {
                // key,屬性名
                setCss(curEle, key, obj[key])
            }
        }
        // setGroupCss(box, {
        //     width: 200,
        //     height: 300,
        //     backgroundColor: 'pink'
        // })

        function css() {
            let curEle = arguments[0];
            let attr = arguments[1];
            let val = arguments[2];
            // 如果arguments的legnth是2,那說明你不是想獲取樣式,就是想成組設置樣式
            if (arguments.length === 2) {
                if (typeof attr === 'string') {
                    // 如果此條件成立,那就說明第二個參數就是字符串,就是想獲取參數
                    return getCss(curEle, attr)
                } else {
                    setGroupCss(curEle, attr)
                }
            }
            // 如果arguments的legnth是3,那說明你是想單個設置樣式
            else if (arguments.length === 3) {
                setCss(curEle, attr, val)
            }
        }
    </script>
</body>

</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章