JavaScript DOM 樣式操做

style 屬性

  1. DOM 不能直接操做 CSS 文件,經過操做元素的 style 屬性間接操做樣式javascript

    var oDiv = document.getElementsByTagName('div')[0];
    oDiv.style.width = '100px';
    oDiv.style.backgroundColor = 'red';
    // style 屬性即內聯樣式,不能得出 CSS 文件定義的內容
    // 若是沒有對應內聯樣式,則返回空字符串
    複製代碼
  2. 注意css

    • 屬性使用小駝峯寫法
    • 賦值使用字符串
    • 複合值最好拆解,有利於提升性能,如 border
    • style.float 使用 style.cssFloat 代替,不衝突保留字

getComputedStyle 方法

  1. window 下的 getComputedStyle,返回元素生效的樣式屬性html

    window.getComputedStyle(elem, null);
    var width = window.getComputedStyle(div, null)['width']; // 返回字符串
    window.getComputedStyle(elem, 'after'); // 返回 after 僞元素的樣式屬性
    複製代碼
  2. getComputedStyle 返回的數據會被轉換成特定的單位,如 em 會轉換爲 px,十六進制顏色會轉換爲 rgb/rgba 等java

  3. IE8 及如下不支持 getComputedStyle,能夠使用 elem.currentStyle 代替markdown

    function getStyles(elem) {
        if (window.getComputedStyle) {
            return window.getComputedStyle(elem, null);
        } else return elem.currentStyle;
    }
    複製代碼

offsetWidth 屬性

  1. offsetWidth、offsetHeight 能夠得到元素的物理尺寸框架

    var oDiv = document.getElementsByTagName('div')[0];
    oDiv.offsetWidth; 
    oDiv.offsetHeight; 
    // offsetWidth = border + padding + width
    複製代碼
  2. offsetWidth、offsetHeight 包括了 padding,對一些開發場景形成不便,最好使用 getComputedStyle 方法性能

僞元素樣式

  1. ::berfore,::after 僞元素的樣式沒法經過方法直接改變動畫

  2. 一般修改關聯元素的 clssName 改變僞元素的樣式ui

    .box1::after{
        content: "";
        background-color: red;
    }
    .box2::after{
        content: "";
        background-color: black;
    }
    複製代碼
    var oDiv = document.getElementsByClassName('box1')[0];
    oDiv.className = 'box2';
    // after 僞元素的樣式也隨之改變
    複製代碼

樣式動畫

  1. 元素運動spa

    經過改變元素的樣式屬性使其顯示內容發生改變,以下拉菜單、側邊抽屜、彈出信息框等

    咱們常用 CSS3 或者一些封裝好的框架來實現動畫,動畫效果能夠給頁面帶來更好的交互體驗

  2. 原生 JS 實現樣式動畫

    • 獲取要運動的元素節點
    • 明確要改變的樣式屬性
    • 肯定動畫時間,動畫速度和動畫終止條件
    • 建立計時器,終止條件下清除計時器
  3. 下拉菜單示例

    html

    <div class="dropdown">
        <a href="javascript:;" class="main">下拉菜單</a>
        <ul class="list">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
    </div>
    <script type="text/javascript"> var dropdown = document.getElementsByClassName('dropdown')[0], oList = dropdown.getElementsByClassName('list')[0], timer = null, listHeight = 0, speed = 2; dropdown.onmouseenter = function () { clearInterval(timer); timer = setInterval(function () { if (listHeight >= 200) { clearInterval(timer); } else { listHeight = parseInt(getStyles(oList)['height']) + speed; oList.style.height = listHeight + 'px'; } }, 1); } dropdown.onmouseleave = function () { clearInterval(timer); timer = setInterval(function () { if (listHeight <= 0) { clearInterval(timer); } else { listHeight = parseInt(getStyles(oList)['height']) - speed; oList.style.height = listHeight + 'px'; } }, 1); } function getStyles(elem) { if (window.getComputedStyle) { return window.getComputedStyle(elem, null); } else return elem.currentStyle; } </script>
    複製代碼

    css

    <style>
            .dropdown {
                width: 100px;
            }
    
            .dropdown .main {
                display: block;
                height: 50px;
                background-color: black;
                color: white;
                text-align: center;
                text-decoration: none;
                line-height: 50px;
            }
    
            .dropdown .list {
                overflow: hidden;
                height: 0;
                margin: 0;
                padding: 0;
                list-style: none;
            }
    
            .list li {
                height: 50px;
                background-color: #999;
                text-align: center;
                line-height: 50px;
            }
    
            .dropdown li:hover {
                background-color: black;
                color: white;
            }
        </style>
    複製代碼
相關文章
相關標籤/搜索