How to make background color of div tag keep changing using JQuery?

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

I am new to JQuery so bare with me: I am trying to change background of div tag continuously using for loop and rgb() values . following is my code written in sample.html:

    <!DOCTYPE html> <html lang="en">     <head>         <title>sample</title>         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">         </script >         <script>         $(document).ready(function(){             var r=0;             var g=0;                 var b=0;              for(r=0;r<255;r++)             {                 for(g=0;g<255;g++)                 {                     for(b=0;b<255;b++)                     {                         $("#sqr").css("background-color","rgb(r,g,b)");                     }                 }             }         });          </script>           <style>         #sqr{background-color:rgb(255,0,0);              height:200px;               width:200px;             }         </style>      </head>     <body>      <div id="sqr">     </div>      </body>     </html>

so can enyone tell me how should i make a code so that background color of div keeps changing automatically when page is loaded ? Note that i want to change color very smoothly.

If you want to see how effects i want then visit:here and see effects its showing.

As per some suggestions i have changed my code to :

$("#sqr").css("background-color","rgb("+r+","+g+","+b+")");

Now it is taking values of rgb correctly ,but not loading effects correctly in browser,shows me dialog box :

So, please suggest me some solution on that to resolve this problem. Thank u in advance . .

回答1:

As this question is tagged as CSS, I would like to contribute a pure CSS solution to this, simply use CSS3 @keyframes and you can simply add colors to the element you want, can step the animation using % and also, you can use animation-iteration-count and set it to infinite. If you want to iterate it for limited times, just changed the infinite to whatever value you desire but make sure it's an integer.

Demo

.animate {     height: 200px;     width: 400px;     border: 1px solid #000;     -webkit-animation: animate_bg 5s;     animation: animate_bg 5s;     -webkit-animation-iteration-count: infinite;     animation-iteration-count: infinite; }  @keyframes animate_bg {     0%   {background:red;}     50%  {background:green;}     100% {background:blue;} }  @-webkit-keyframes animate_bg {     0%   {background:red;}     50%  {background:green;}     100% {background:blue;} }

This is introduced in CSS3 spec, so you can refer this link for browser support.


There are many polyfills available for the browsers which are not supporting CSS3 @keyframes, so you may need to use this if you are looking to support older browsers, if you don't care about the old one's, than you can use this without any hesitation.



回答2:

Everything seems fine besides the code where you set the CSS.

Change the code :

$("#sqr").css("background-color","rgb(r,g,b)");

To :

$("#sqr").css("background-color","rgb(" + r + "," + g + "," + b + ")");

This will now pass in the number values, i.e.

$("#sqr").css("background-color","rgb(254,254,254)");

Where as before it would have simply passed the values r,g,b because you placed them within a string.



回答3:

You can use .setInterval()

Enclose your code within a function called animate

var intervalID = window.setInterval(animate, 500);

Not sure correct this

$("#sqr").css("background-color","rgb(" + r +"," +g+"," +b +")");


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!