JavaScript DOM-style operations

Keywords: Javascript Attribute css3

JavaScript style operations include controlling the inline style of an element through the style attribute, getting the effective style of an element through the getComputedStyle method, changing the pseudo element style by changing the element class name, and animating the style through a timer

style attribute

  1. DOM cannot manipulate CSS files directly, indirectly by manipulating style attributes of elements

    var oDiv = document.getElementsByTagName('div')[0];
    oDiv.style.width = '100px';
    oDiv.style.backgroundColor = 'red';
    // Style attribute is an inline style and cannot derive the contents of a CSS file definition
    // If there is no corresponding inline style, an empty string is returned
    
  2. Be careful

    • Attribute using hump notation
    • Assignment uses string
    • Composite values are best disassembled to improve performance, such as border
    • style.float uses style.cssFloat instead of conflict-free reserved words

getComputedStyle method

  1. getComputedStyle under window, returns the style attributes that the element takes effect

    window.getComputedStyle(elem, null);
    var width = window.getComputedStyle(div, null)['width']; // Return string
    window.getComputedStyle(elem, 'after'); // Returns the style attributes of the after pseudo element
    
  2. The data returned by getComputedStyle is converted to specific units, such as em to px, hexadecimal color to rgb/rgba, etc.

  3. getComputedStyle is not supported in IE8 and below and can be replaced with elem.currentStyle

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

offsetWidth property

  1. offsetWidth, offsetHeight can get the physical size of an element

    var oDiv = document.getElementsByTagName('div')[0];
    oDiv.offsetWidth; 
    oDiv.offsetHeight; 
    // offsetWidth = border + padding + width
    
  2. offsetWidth, offsetHeight include padding, which is inconvenient for some development scenarios. It is best to use the getComputedStyle method

Pseudo Element Style

  1. :: berfore,:: after pseudo-element style cannot be directly changed by method

  2. Changing the style of pseudo elements is usually done by modifying the clssName of the associated element

    .box1::after{
        content: "";
        background-color: red;
    }
    .box2::after{
        content: "";
        background-color: black;
    }
    
    var oDiv = document.getElementsByClassName('box1')[0];
    oDiv.className = 'box2';
    // The style of the after pseudo element also changes
    

Style Animation

  1. Elemental Motion

    Change an element's display content by changing its style properties, such as drop-down menus, side drawers, pop-up information boxes, and so on

    We often use CSS3 or some encapsulated frames to animate, and animation effects can provide a better interactive experience for the page

  2. Style animation with native JS

    • Get the element node to move
    • Style properties that are explicitly to be changed
    • Determine animation time, animation speed, and animation termination conditions
    • Create timer, clear timer under termination condition
  3. Example drop-down menu

    html

    <div class="dropdown">
        <a href="javascript:;" class="main">drop-down menu</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>
    

Posted by NSH on Sun, 05 Apr 2020 18:38:21 -0700