1. 程式人生 > >給偽元素的css屬性動態賦值以及獲取css屬性值

給偽元素的css屬性動態賦值以及獲取css屬性值

一/ 設定值

就是動態新增style

$('head').append($('<style class="styleBefore">.leveltwo-scroll::before{height:' + domRight + 'px;}</style>'));

二/ 獲取值

#leveltwo-scroll::before {
            content: 'hai';
            position: absolute;
            top: 0;
            left: 7px;
            width: 0.5px;
            height: 0px;
            background-color: #ccc;
        }
//獲取值
var myIdElement = document.getElementById("leveltwo-scroll");
var beforeStyle = window.getComputedStyle(myIdElement, ":before");
console.log(beforeStyle); // [CSSStyleDeclaration Object]
console.log(beforeStyle.width); // 0.5px
console.log(beforeStyle.getPropertyValue("width")); // 0.5px
console.log(beforeStyle.content); // "hai"

參考文章