操作:在使用jQuery的伪类背景之前

时间:2016-12-22 21:05:46

标签: javascript jquery

我已经在StackOverflow上看到了几个解决方案,但是对于我想要实现的目标,似乎没有一个能够解决这个问题。

现场演示:



var value = 0,
    pos = 0,
    progressHidden = false,
    progressEl = $('progress'),
    timer = setInterval(progress, 100);

var ProgressColourTween = [
    "#ff4400", "#ff4900", "#ff4a00", "#ff4c00", "#ff4d00", "#ff4f00", "#ff5000", "#ff5100", "#ff5200", "#ff5400",
    "#ff5600", "#ff5700", "#ff5900", "#ff5a00", "#ff5c00", "#ff5c00", "#ff5e00", "#ff5f00", "#ff6100", "#ff6300",
    "#ff6400", "#ff6600", "#ff6700", "#ff6800", "#ff6900", "#ff6b00", "#ff6c00", "#ff6e00", "#ff6f00", "#ff7100",
    "#ff7200", "#ff7300", "#ff7500", "#ff7600", "#ff7800", "#ff7900", "#ff7a00", "#ff7c00", "#ff7d00", "#ff7f00",
    "#ff8700", "#ff8800", "#ff8a00", "#ff8b00", "#ff8c00", "#ff8e00", "#ff8f00", "#ff9100", "#ff9200", "#ff9400",
    "#ff9400", "#fc9500", "#fa9600", "#f79700", "#f59800", "#f29900", "#f09a00", "#ed9c00", "#eb9c00", "#e89e00",
    "#e69e00", "#e3a000", "#e0a100", "#dea200", "#dba300", "#d9a400", "#d6a500", "#d4a600", "#d1a700", "#cfa800",
    "#cca900", "#c9ab00", "#c7ab00", "#c4ad00", "#c2ae00", "#bfaf00", "#bdb000", "#bab100", "#b8b200", "#b5b300",
    "#b3b400", "#b0b500", "#adb600", "#abb700", "#a8b900", "#a6b900", "#a3bb00", "#a1bb00", "#9ebd00", "#9cbe00",
    "#99bf00", "#96c000", "#94c100", "#91c200", "#8fc300", "#8cc400", "#8ac500", "#87c600", "#85c700", "#82c800"
];

function progress() {

    // run counter
    value++;
    if (value <= 100) {
        progressEl.val(value);
        progressEl.css("background-color", ProgressColourTween[value]);
        pos = 1 - (value / 100);
        if (value == '10') {
            $('progress').addClass('ten');
        }
        if (value == '100') {
            $('progress').addClass('hundred');
        }
    }
    // update background
    progressEl.css('background-position', '0 ' + pos + 'em');

    // show/hide progress
    if (progressHidden && value < 100) {
        progressEl.val(0);
        progressEl.removeClass("hidden");
        progressHidden = false;
    }
}
&#13;
body {
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    padding: 50px;
    background: #000000;
}

progress {
    appearance: none;
    position: relative;
    width: 450px;
    height: 1.4em;
    padding: 0;
    border: none;
    font-family: "Comic Sans MS", cursive, sans-serif;
    font-size: 120px;
    transition: height .4s;
}

progress.hidden {
    height: 0;
    transition-delay: .4s;
}

progress::before {
    content: attr(value);
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    text-align: center;
    font-size: 1.5em;
    line-height: .80em;
    color: hsla(0, 0%, 100%, .2);
    background: linear-gradient( green, orange) no-repeat center;
    background: -moz-linear-gradient( green, orange) no-repeat center;
    background: -webkit-linear-gradient( green, orange) no-repeat center;
    background: -o-linear-gradient( green, orange) no-repeat center;
    background: -ms-linear-gradient( green, orange) no-repeat center;
    background-position: inherit;
    -webkit-background-clip: text;
    background-clip: text;
}

progress::after {
    content: '%';
    position: absolute;
    top: 0;
    right: 190px;
    bottom: 0;
    text-align: center;
    font-size: 1.5em;
    line-height: .80em;
    color: hsla(0, 0%, 100%, .2);
    background: linear-gradient( green, orange) no-repeat center;
    background: -moz-linear-gradient( green, orange) no-repeat center;
    background: -webkit-linear-gradient( green, orange) no-repeat center;
    background: -o-linear-gradient( green, orange) no-repeat center;
    background: -ms-linear-gradient( green, orange) no-repeat center;
    background-position: inherit;
    -webkit-background-clip: text;
    background-clip: text;
}

progress.ten::after {
    right: 100px;
}

progress.hundred::after {
    right: 0px;
}

progress::-webkit-progress-bar,
progress::-webkit-progress-value {
    background: transparent;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<progress max="99"></progress>
&#13;
&#13;
&#13;

我的问题

我已将我的颜色补间添加到progressEl = $('progress'),但是它需要以某种方式应用于.progress:before

问题

如何将补间颜色添加到通过.progress:before背景操作的加载百分比文本中?

2 个答案:

答案 0 :(得分:0)

无法说出$('.progress:before'),但您可以在样式表中使用addClass()为您的progress元素添加另一个类。或者你可以这样做:

var porgress_before = $("<style>").appendTo("head");

// And then you can use below function on any where to style your element. 
porgress_before.text(".progress:before{ background: red;}");
祝你好运!

答案 1 :(得分:0)

没有时间为您编写代码,但您可以在头部底部动态创建样式标记,然后创建并插入适用于:before元素的新规则。 https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule

这将传回一个索引值,您可以使用该索引值来查找实际的CSSStyleRule对象。然后,您可以像操作元素的样式属性一样操纵该对象的样式属性 https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleRule