Animate background color like a progress bar in jQuery

前端 未结 11 2168
南笙
南笙 2021-02-05 23:11

I have a div which has some text content( eg: My Name ) and the div is in RED background colour

相关标签:
11条回答
  • 2021-02-05 23:56
    1. JavaScript variable names can't have dashes, it should be backgroundColor.
    2. You have 'red', so it will not change anything. Change the string to 'blue'.
    $("button").click(function(){ 
          $('#myDivId').animate({ backgroundColor: 'blue' }, 10000);
    });
    
    0 讨论(0)
  • 2021-02-06 00:01

    you can use this code:

    jsFiddle is here

    HTML:

    <button>Button</button>
    <div id='container'>
        <div class="progress"></div>
        <div class="content">
            test Content
        </div>
    </div>
    

    CSS:

    #container{
        background:#eee;
        color:#555; 
        padding:10px; 
        overflow:hidden; 
        position:relative;
    }
    .content{
        z-index:9;
        position:relative;
    }
    .progress{
        z-index;1;
        width:0px;
        height:100%;
        position:absolute;
        background:tomato;
        left:0px;
        top:0px;
    }
    .filling{
    animation: fill 10s linear;
    -webkit-animation: fill 10s; /* Safari and Chrome */
    animation-timing-function:linear;
    -webkit-animation-timing-function:linear; /* Safari and Chrome */
    }
    
    @keyframes fill
    {
    0%   {background: red; width:0%;}
    25%  {background: yellow; width:25%;}
    50%  {background: blue; width:50%;}
    75%  {background: green; width:75%;}
    100% {background: lightgreen; width:100%;}
    }
    
    @-webkit-keyframes fill /* Safari and Chrome */
    {
    0%   {background: red; width:0%;}
    25%  {background: yellow; width:25%;}
    50%  {background: blue; width:50%;}
    75%  {background: green; width:75%;}
    100% {background: lightgreen; width:100%;}
    }
    

    JQuery:

    $("button").click(function(){ 
          $('.progress').addClass("filling").css("background","lightgreen").width("100%");
    });
    
    0 讨论(0)
  • 2021-02-06 00:01

    You need to use the jQuery.Color() plugin for background-color animation.

    0 讨论(0)
  • 2021-02-06 00:07

    If you are able to use latest CSS (which mean no stupid old browser), you may use CSS3's gradient function

    $("#bar").mousemove(function (e) {
        var now=e.offsetX/5;
        $(this).css('backgroundImage', 'linear-gradient(to right, #00f 0%,#00f ' + now + '%,#f00 ' + now + '%,#f00 100%)');
    }).click(function(e){
        e.preventDefault();
        var start=new Date().getTime(),
            end=start+1000*10;
        function callback(){
            var now=new Date().getTime(),
                i=((now-start) / (end-start))*100;
            $("#bar").css('backgroundImage', 'linear-gradient(to right, #00f 0%,#00f ' + i + '%,#f00 ' + i + '%,#f00 100%)');
            if(now<end){
                requestAnimationFrame(callback, 10);
            }
        };
        requestAnimationFrame(callback, 10);
    });
    

    Example at: http://jsfiddle.net/hkdennis2k/ebaF8/2/

    0 讨论(0)
  • 2021-02-06 00:08

    Background gradient loader - possibly more appropriate

    You could also use the background gradient as the loader. Of course, jQuery doesn't natively support choosing correct CSS prefixes, so it may have to be tweeked to work in older browsers. But it'll work nicely where your browser supports linear-gradient.

    Since jQuery won't animate a background gradient, I've animated a span within it and am using the step option to change the gradient stops each time. This means that any duration or easing changes to the animate() will apply to the gradient as well:

    $("button").click(function(){    
        $('#loadContainer span').animate({width:'100%'}, {
            duration:10000,
            easing:'linear',
            step:function(a,b){
                $(this).parent().css({
                    background:'linear-gradient(to right, #0000ff 0%,#0000ff '+a+'%,#ff0000 '+a+'%,#ff0000 100%)'
                });
            }
        });
    });
    

    JSFiddle


    Original answer

    background-color is the CSS style, you are targeting the property of the javascript object, which in this case, is backgroundColor. You'll want to change the color name as well.

    $("button").click(function(){ 
        $('#myDivId').animate({backgroundColor:'blue'},10000);
    });
    

    JSFiddle

    0 讨论(0)
提交回复
热议问题