Animate background color like a progress bar in jQuery

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

    //this is the only additional line you need to add

    Progress 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%");
    });
    

提交回复
热议问题