JS 颜色渐变动画

 
    <input id="Button1" type="button" value="button" οnclick="tggg()" />
    <script>
        function tggg() {
            //$("#asd").css({ "background-color": "red" }).show().fadeOut(500);
            fadeColor(
            { r: 0, g: 255, b: 0 }, //star color   
            {r: 255, g: 255, b: 255 }, //end color   
            function (color) { document.getElementById("asd").style.backgroundColor = color; }, 1, 10);
        }


        //所有代码的执行时间只有24毫秒左右。
        function fadeColor(from, to, callback, duration, totalFrames) {
            //用一个函数来包裹setTimeout,根据帧数来确定延时   
            function doTimeout(color, frame) {
                setTimeout(function () {
                    try {
                        callback(color);
                    } catch (e) { JSLog.write(e); }
                }, (duration * 1000 / totalFrames) * frame);
                //总持续秒数/每秒帧数*当前帧数=延时(秒),再乘以1000作为延时(毫秒)    
            }
            // 整个渐变过程的持续时间,默认为1秒   
            var duration = duration || 1;
            // 总帧数,默认为持续秒数*15帧,也即每秒15帧    
            var totalFrames = totalFrames || duration * 15; var r, g, b; var frame = 1;
            //在第0帧设置起始颜色    
            doTimeout('rgb(' + from.r + ',' + from.g + ',' + from.b + ')', 0);
            //计算每次变化所需要改变的rgb值    
            while (frame < totalFrames + 1) {
                r = Math.ceil(from.r * ((totalFrames - frame) / totalFrames) + to.r * (frame / totalFrames));
                g = Math.ceil(from.g * ((totalFrames - frame) / totalFrames) + to.g * (frame / totalFrames));
                b = Math.ceil(from.b * ((totalFrames - frame) / totalFrames) + to.b * (frame / totalFrames));
                // 调用本frame的doTimeout        
                doTimeout('rgb(' + r + ',' + g + ',' + b + ')', frame); frame++;
            }
        }
    </script>
    <div style="width: 600px; height: 200px; border: 1px solid red;" id="asd">
        asdasdasdsadasdasdsadasdas asd阿大使大赛的撒撒阿斯
    </div>


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值