Animate background color like a progress bar in jQuery

前端 未结 11 2186
南笙
南笙 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:43

    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
    >             });            
    >         }
    >     })
    >      });
    
    0 讨论(0)
  • 2021-02-05 23:43

    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 .......

    0 讨论(0)
  • 2021-02-05 23:45

    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>
    
    0 讨论(0)
  • 2021-02-05 23:48

    I thought your looking for a "Progress Bar Animation"? try this: i believe its what your looking for - the progress bar's horizontal loading motion in a jsfiddle here:

    http://jsfiddle.net/c78vF/6/

    with a few more elements you are able to simulate that easily using the same tech everyone else is using here... jquery ui etc

    html:

    <button>Button</button>
    <div id='myDivId'>
        <div class="progress"></div>
        <div class="content">
            DIV
        </div>
    </div>
    

    css:

    #myDivId{
        background:red; 
        color:white; 
        padding:10px; 
        overflow:hidden; 
        position:relative;
    }
    
    .content{
        z-index:9;
        position:relative;
    }
    .progress{
        z-index;1;
        width:0px;
        height:100%;
        position:absolute;
        background:blue;
        left:0px;
        top:0px;
    }
    

    js:

    $("button").click(function(){ 
          $('.progress').animate({ width: '100%' }, 10000);
    });
    
    0 讨论(0)
  • 2021-02-05 23:50

    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).

    0 讨论(0)
  • 2021-02-05 23:53

    This can be done using span changing its width over the time. Here is the working fiddle. The code is displayed below. Only the mandatory styles are displayed here.

    HTML

    <div class="red-button">
        <span class="bg-fix"></span>  //this is the only additional line you need to add
        <p>Progress Button</p>
    </div>
    <button class="click-me">Click Me</button>
    

    CSS

    .red-button {
        position: relative;
        background: red;
    }
    span.bg-fix {
        display: block;
        height: 100%;
        width: 0;
        position: absolute;
        top: 0;
        left: 0;
        background: blue;
        transition: width 10s ease-in-out;
    }
    p {
        position: relative;
        z-index: 1
        width: 100%;
        text-align: center;
        margin: 0;
        padding: 0;
    }
    

    jQuery

    $("div.red-button").click(function () {
      $('span.bg-fix').css("width", "100%");
    });
    
    0 讨论(0)
提交回复
热议问题