Animate background color like a progress bar in jQuery

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

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

提交回复
热议问题