我有一个元素,其:before样式必须根据计算进行修改。
:before
export class Content extends React.Component { render() { return ( <div className="ring-base"> <div className="ring-left" style={{/* Change here */}}></div> <div className="ring-right" style={{/* Change here */}}></div> <div className="ring-cover"> <div className="ring-text">10%</div> </div> </div> ); } }
CSS代码:
.ring-base { position: absolute; height: 200px; width: 200px; border-radius: 50%; background: red; transform: rotate(90deg); overflow:hidden; } .ring-cover { position: absolute; height: 180px; width: 180px; background: #fff; border-radius: 50%; top: 5%; left: 5%; } .ring-cover .ring-text { position: absolute; width: 100%; height: 100%; text-align: center; font-size: 2em; display: flex; justify-content:center; align-content:center; flex-direction:column; transform: rotate(-90deg); } .ring-right, .ring-left { height: 100px; width: 200px; overflow: hidden; position: absolute; } .ring-right:before, .ring-left:before { height: inherit; width: inherit; position: absolute; content: ""; border-radius: 100px 100px 0 0; background-color: grey; transform: rotate(0deg); } .ring-right { -webkit-transform-origin: 50% 0%; -moz-transform-origin: 50% 0%; -ms-transform-origin: 50% 0%; transform-origin: 50% 0%; transform: rotateZ(0deg); } .ring-left { transform: rotate(180deg); -webkit-transform-origin: 50% 100%; -moz-transform-origin: 50% 100%; -ms-transform-origin: 50% 100%; transform-origin: 50% 100%; } .ring-right:before { -webkit-transform-origin: 50% 100%; -moz-transform-origin: 50% 100%; -ms-transform-origin: 50% 100%; transform-origin: 50% 100%; transform: rotate(0deg); } .ring-left:before { -webkit-transform-origin: 50% 100%; -moz-transform-origin: 50% 100%; -ms-transform-origin: 50% 100%; transform-origin: 50% 100%; -webkit-transform: rotate(0deg); -moz-transform: rotate(0deg); -ms-transform: rotate(0deg); transform: rotate(0deg); }
要求是能够通过ReactJS .ring-left:before和.ring-right:before通过ReactJS 更新transform属性。
.ring-left:before
.ring-right:before
如果有一种方法可以不更新:before类,也可以完全不使用CSS :before,则也建议这样做。
您可以重复document.styleSheets设置.style的.cssRules地方.selectorText比赛伪选择
document.styleSheets
.style
.cssRules
.selectorText
let sheets = document.styleSheets; let selector = "div::before"; let replacementContent = '"after"'; for (let sheet of sheets) { for (let rule of sheet.cssRules) { if (rule.selectorText === selector) { rule.style["content"] = replacementContent; } } } div:before { content: "before"; color: red; font-weight: bold; text-align: center; text-shadow: 2px 2px 2px #000; background: green; width: 50px; height: 50px; display: block; } <div></div>