简体   繁体   English

如何使用jquery或css更改滑块的背景颜色

[英]how can I change the background color of slider using jquery or css

Will u please tell me how can i change the background color of the slider using jquery and css when it slides. 您能告诉我如何在滑动时使用jquery和css更改滑动器的背景颜色。

I have used the method 我用过的方法

.ui-slider-range{background:colorName}; .ui-slider-range {background:colorName};

I hope this helps. 我希望这有帮助。

There is also a demo of this example. 此示例也有一个演示

$(document).ready(function() {
    $('.slide').slider({
        slide: function(event, ui) {
            // from pure red to pure green
            var r, g, b = 0;
            g = Math.round(255 / 100 * ui.value); // 2.55 * (0 - 100) = 0 - 255
            r = 255 - g;
            var color = 'rgb(' + r + ',' + g + ',' + b + ')';
            $(this).css({
                'background': color
            });
        }
    });
});

You can find algorithms for color lines out there somewhere ;-) This one I just made up. 您可以在某处找到用于颜色线的算法;-)这是我刚刚编写的。

ps I've added another "algorithm" into demo ;-) ps我在演示中添加了另一个“算法” ;-)

This might be useful. 这可能很有用。 It's using JQuery to alter the css. 它使用JQuery更改CSS。 all of my attempts, in my case, to set the CSS on the actual DIV, where the slider resided, failed? 就我而言,所有尝试在滑块所在的实际DIV上设置CSS的尝试都失败了吗? I guess the JQuery CSS libraries took precedence? 我猜JQuery CSS库优先? It seems like over-kill. 好像太杀了。 But this worked for me. 但这对我有用。

    var cs_style = "<style>#your_slider_div .ui-widget-header { background: #7D1515; } </style>";
    $('html > head').append(cs_style);

Running this ABOVE the jQuery function (definition and call) for your slider will set the background colour to a dark red colour. 在滑块上运行jQuery函数(定义和调用),将背景色设置为深红色。 Of course, you can set it to what-ever you like. 当然,您可以将其设置为任意值。 More than one way to crack the nut eh? 破解螺母的方法不止一种吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM