给伪元素的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"

参考文章

猜你喜欢

转载自blog.csdn.net/zhaohanqq/article/details/85012234
今日推荐