1

i wrote a code for fading effect on a div in jquery.

$("#trigger").click(function(e){
    $("#divid").parent().css("background-color","#D18F8F");
    console.log($("#divid").parent().css("background-color"));
    e.preventDefault();
    $('html, body').animate({scrollTop:$("#divid").offset().top},'slow');
    $("#divid").parent().css("transition","background-color 2s linear");
    $("#divid").parent().css("background-color","transparent");
});

when first time i clicked on #trigger it worked as expected, but next time i clicked on trigger it is just not working. here is console output rgb(209, 143, 143) for the first time and rgba(0,0,0,0) for all next time.

EDIT: here is the fiddle

2
  • In witch browser are you working? Commented Nov 17, 2013 at 20:01
  • @AlexandreDiacov Chrome 30 Commented Nov 17, 2013 at 20:02

2 Answers 2

2

To set your background color to #D18F18F fast you have to remove the transition. Else wise you would have to wait 2s again before #divid has the new background color.

Add this at the first line of your .click function:

$("#divid").parent().css("transition","none");
Sign up to request clarification or add additional context in comments.

2 Comments

but in my problem, i waited for 2 sec and this is not showing again
@optional optional It requires 2s from the moment you click on the trigger. @ A. Wolff how can you edit my answer? (And how do I insert code the right way, the code block didn't seem to work when I tried it).
0

Add jQuery-UI and you can use.......

$("#trigger").click(function(e){
    $('html, body').animate({scrollTop:$("#divid").offset().top},'slow');
    $(".abc").animate({backgroundColor:"#D18F8F"},2000);
    $(".abc").animate({backgroundColor:"#FFFFFF"});
});

jsFiddle

2 Comments

You should say: <<If you are already using jQuery UI, you can use...>>
He did not have it added as a library in his fiddle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.