溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用jQuery怎么實現(xiàn)一個背景顏色漸變動畫效果

發(fā)布時間:2021-04-15 16:37:47 來源:億速云 閱讀:262 作者:Leah 欄目:web開發(fā)

使用jQuery怎么實現(xiàn)一個背景顏色漸變動畫效果?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

完整實例代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>背景顏色漸變</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
</head>
<body>
<input id="Button1" type="button" value="button" onclick="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);
  }
  //所有代碼的執(zhí)行時間只有24毫秒左右。
  function fadeColor(from, to, callback, duration, totalFrames) {
    //用一個函數(shù)來包裹setTimeout,根據(jù)幀數(shù)來確定延時
    function doTimeout(color, frame) {
      setTimeout(function () {
        try {
          callback(color);
        } catch (e) { JSLog.write(e); }
      }, (duration * 1000 / totalFrames) * frame);
      //總持續(xù)秒數(shù)/每秒幀數(shù)*當前幀數(shù)=延時(秒),再乘以1000作為延時(毫秒)
    }
    // 整個漸變過程的持續(xù)時間,默認為1秒
    var duration = duration || 1;
    // 總幀數(shù),默認為持續(xù)秒數(shù)*15幀,也即每秒15幀
    var totalFrames = totalFrames || duration * 15; var r, g, b; var frame = 1;
    //在第0幀設(shè)置起始顏色
    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));
      // 調(diào)用本frame的doTimeout
      doTimeout('rgb(' + r + ',' + g + ',' + b + ')', frame); frame++;
    }
  }
</script>
<div  id="asd">
  億速云歡迎各位光臨--https://www.jb51.net
</div>
</body>
</html>

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI