【发布时间】:2018-08-12 20:24:17
【问题描述】:
如何为伪元素样式更改 CSS?
我正在尝试获取 CSS before:: 规则并将 left: 更改为 95% 或 4px。
如何在我的上下文中执行此操作?
我还使用document.querySelector 进行了一些测试,但它不起作用,我得到一个计算只读错误。
你有什么建议吗?
例子:
css
.iziToast-wrapper-bottomLeft .iziToast.iziToast-balloon:before {
border-right: 15px solid transparent;
border-left: 0 solid transparent;
right: auto;
left: 8px;
}
js
if(description_iziToast){
let RightMode = event.x>window.innerWidth/2;
let bubblePosition = document.getElementsByClassName("iziToast-balloon")[0]; // get the div that hold the bubble
let ajustScreenLR = RightMode && document.getElementsByClassName("iziToast")[0].offsetWidth || 0; // halfScreen ajustement
//bubblePosition.style.left = RightMode && '95%' || '4px'; // here i need to change the position in the befor:: attribut
description_iziToast.style.left = `${event.x-20-ajustScreenLR}px`;
description_iziToast.style.top = `${event.y-105}px`;
}
}else{
if(description_iziToast){ iziToast.destroy(); description_iziToast = false; };
}
这里是应用控制台调试
【问题讨论】:
-
如果真的只是左右两个固定值的切换,那么只需在元素上添加一个'rightMode'类,在css中做
elem.rightMode::before { left: 95%;} -
我想避免在需要更新时修改原始 css 代码。我可能会以这种方式进行。
标签: javascript css dom pseudo-element