Animate background color like a progress bar in jQ

2019-03-15 10:23发布

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

My need :

If I have clicked the button , I need to change the background of a div from RED to BLUE like Progress bar for 10 seconds.Something like,

start from 0 sec

=

==

===

====

=====

======

=======

========

=========

==========

end with 10 seconds

I have to change the bgColor gradually from start to end for upto 10 secs.

So I have used the JQuery animate() method .But I have no luck to do that.

What I have tried :

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

If this is not possible , can anyone please suggest me some plugin's to do that.

Hope our stack users will help me.

11条回答
ら.Afraid
2楼-- · 2019-03-15 11:17

try below code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>


     $(document).ready(function(){

        $("#button").click(function(){ 
            $('#myDivId').css("background-color","BLUE");
            $( "#effect" ).animate({
            backgroundColor: "yellow", }, 1000 );
      });
    });

</script>


<div id="myDivId" style="width:120;height:130;background-color:RED;text-align: center;">Text Area</div>

<input type="button" id="button" class="" name="click" value="click" >

And Let me know it's working or not...waiting for your reply .......

查看更多
太酷不给撩
3楼-- · 2019-03-15 11:17

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%");
});
查看更多
爷的心禁止访问
4楼-- · 2019-03-15 11:17

Any particular reason you want to do this in jQuery as opposed to css3?

Bootstrap has a pretty nifty way of achieving this -> http://getbootstrap.com/components/#progress-animated

The .progress-bar class has a css3 transition set on the width property, so as you move between 0% & 100%, any updates to the width are smoothly animated. It also uses a css3 animation to animate the background gradient (striped pattern).

查看更多
老娘就宠你
5楼-- · 2019-03-15 11:19

Or you could do the math and do the color change. http://jsfiddle.net/rustyDroid/WRExt/2/

> $("#btn").click(function(){
>     $('#bar').animate({ width: '300px' }, 
>     {
>         duration:10000,
>         easing:'linear',
>         step:function(a,b){
>             var pc = Math.ceil(b.now*255/b.end);
>             var blue = pc.toString(16);
>             var red = (255-pc).toString(16);
>             var rgb=red+"00"+blue;
>             $('#bar').css({
>                 backgroundColor:'#'+rgb
>             });            
>         }
>     })
>      });
查看更多
疯言疯语
6楼-- · 2019-03-15 11:19

Furthemore of the previus coments (change background-color for backgroundColor) you also need the plugin Jquery Colors, I made a test and without it don't work.

Put this on the head:

  <script src="http://code.jquery.com/color/jquery.color.plus-names-2.1.0.min.js"></script>
查看更多
登录 后发表回答