jQuery backgroundColor的animate效果

來源:互聯網
上載者:User

標籤:io   color   os   ar   java   sp   on   問題   cti   

我們知道jQuery幾乎是我們最常用的javascript庫了,不過儘管他自己本身擁有大量的特效,但卻仍然缺少一些動畫效果。比如說,顏色、背景顏色的變換。

animate一般只支援大小,位置,透明度的變化

今天看到新浪微博在微博發布框無內容時,點擊發布按鈕,發布框會有背景顏色閃爍,產生警示的效果,友好的使用者體驗就想模仿一下。 
於是又糾結了一天。。。 

剛開始我用jQuery的fadeIn和fadeOut,很快的就能達到效果。但是我發現,微博那裡是逐漸地改變背景顏色實現的,而fadeIn跟fadeOut是通過逐漸改變alpha即透明度實現。所以開始糾結中。。 
然後,寫了段代碼 

var color = function(){};color.fadeIn = function(a){if(a>=255){$(‘#input‘).css(‘backgroundColor‘, ‘gba(‘+a+‘,‘+a+‘,‘+a+‘)‘);a++;setTimeout(color.fadeIn(a), 20);} else {setTimeout(color.fadeOut(a), 20);}}color.fadeOut = function(a){// 此處省略}


簡單說,我是想實現,比如rgb(a, a, a)遞增至rgb(255, 255, 255) 再遞減至 rgb(a, a, a) 
但是效果差很多,一測試顏色沒變化。 
於是,打了一段測試代碼 

setTimeout($(‘#input‘).css(‘backgroundColor‘, ‘red‘), 1000);setTimeout($(‘#input‘).css(‘backgroundColor‘, ‘black‘), 2000);


開啟瀏覽器,直接就是黑色,並沒有從紅色跳到黑色。 
。。。 
後來 找到jQuery的一個外掛程式叫jquery.color.js裡面重寫了backgroundColor,color,boderColor等屬性顏色的animate效果 
於是問題可以這樣解決 : 
$(‘#input‘).animate({backgroundColor:‘red‘}, 1000); 
這樣就可以從原始的背景顏色逐漸層成紅色的效果。 
很神奇啊,開始研究jquery.color.js的原始碼。 
先附上用jquery.color.js實現的顏色閃爍的警示效果代碼 

$(‘.publish a‘).click(function(){var content = $(‘textarea‘).val();if(content==""){$(‘#input‘).stop(false, true).animate({backgroundColor:‘#FF9966‘}, 250).animate({backgroundColor:‘#FFF‘}, 200).animate({backgroundColor:‘#FF9966‘}, 250).animate({backgroundColor:‘#FFF‘}, 200);} else {$.post(url, {input:content},function(data){});}});

jQuery backgroundColor的animate效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.