4

I can't find a simple solution to this problem.

I am trying to fade in a color change for some text when there is an error from a button click.

if (document.getElementById("username").value == ""){ //First Name      
    $("#login_error").text("Please enetr a username!").css('color', '#FF0000');     
    $("#username_label").css('color', '#FF0000');fadeIn(3000);
}

I can get the color to change but it is instant. I am wanting to get the change of colour to fade in slowly.

Thanks Guys!

Goran Obradovic
  • 8,951
  • 9
  • 50
  • 79
Samuel Meddows
  • 36,162
  • 12
  • 38
  • 36
  • possible duplicate: http://stackoverflow.com/questions/6320578/can-i-fade-in-a-color-with-jquery – JMax Sep 26 '11 at 13:31
  • is the ; between .css() and fadeIn() in line 3 a typo? If not, change it to `.css().fadeIn()` – dnagirl Sep 26 '11 at 13:32

5 Answers5

9

If you add jQuery UI, they added support for color animations.

Read http://jqueryui.com/demos/animate/ for details.

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

  <script>
  $(document).ready(function(){
    $(".block").toggle(function() {
      $(this).animate({ color: "#FF0000" }, 1000);
    },function() {
      $(this).animate({ color: "#000000" }, 1000);
    });
  });
  </script>

Update: jQuery now offers a color plugin to enable only this feature without including the entire jQuery-UI library.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
1

You need to use plugin, it's not part of the built in animation: https://github.com/jquery/jquery-color

Related question: jQuery animate backgroundColor

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

I find the method to change the color and found this

https://stackoverflow.com/a/15234173/1128331

already test and it work what I want to :)

Community
  • 1
  • 1
user1128331
  • 707
  • 4
  • 10
  • 20
0

of course .css call is instant, and fadeIn is just chained (started after css is changed)

try using animate

Guard
  • 6,816
  • 4
  • 38
  • 58
0

You can animate css properties using jQuery .animate function, i.e.

$(this).animate({ height: 250 });

BUT, you cannot animate color, sorry. For that you need to use plugin, see this answer for details.

Also, if you are using jquery UI, then it is possible:

Note: The jQuery UI project extends the .animate() method by allowing some non-numeric styles such as colors to be animated. The project also includes mechanisms for specifying animations through CSS classes rather than individual attributes.

(from http://api.jquery.com/animate/)

You have sample here.

Community
  • 1
  • 1
Goran Obradovic
  • 8,951
  • 9
  • 50
  • 79