【问题标题】:Pseudo-element CSS dynamically using JavaScript使用 JavaScript 动态的伪元素 CSS
【发布时间】:2011-08-23 16:59:38
【问题描述】:

是否可以动态设置伪元素的 CSS?例如:

jQuery 动态样式化帮助容器

$('#help').css({
    "width" : windowWidth - xOffset,
    "height" : windowHeight - yOffset,
    "bottom" : -windowHeight,
    "left" : 200
});

jQuery 尝试设置帮助容器的内边框:

$('#help:before').css({
    "width" : windowWidth - xOffset,
    "height" : windowHeight - yOffset
});

上述的 CSS 文件

#help
{
    opacity: 0.9;
    filter:alpha(opacity=90);   
    -moz-opacity: 0.9;          
    z-index: 1000000;
    bottom: -550px;
    left: 400px;
    background-color: #808080;
    border: 5px dashed #494949;
    -webkit-border-radius: 20px 20px 20px 20px;
    -moz-border-radius: 20px 20px 20px 20px;
    border-radius: 20px 20px 20px 20px;        
}
#help:before 
{
    border: 5px solid white;
    content: '';
    position: absolute;
    -webkit-border-radius: 20px 20px 20px 20px;
    -moz-border-radius: 20px 20px 20px 20px;
    border-radius: 20px 20px 20px 20px;          
}

【问题讨论】:

标签: jquery html css pseudo-element


【解决方案1】:

你不能直接通过jQuery来做。

看这个问题:Setting CSS pseudo-class rules from JavaScript

@Box9's answer 可能是您实际使用的那个:

我拼凑了一个small library for this,因为我确实认为那里 是在 JS 中操作样式表的有效用例。

【讨论】:

    【解决方案2】:

    不要使用.css()设置宽度和高度,您应该直接使用.width()height()

    【讨论】:

    • 你能贴出代码吗,我的代码不起作用:$('#help:before').width(windowHeight - 410) .height(windowHeight - 210);
    猜你喜欢
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多