views:

107

answers:

3

I am wondering for some time now, how did SO do this animation on answers when you click on link answer, answer divs color changes for some time and it reverts back, I'm sure this question has been asked before but I just couldn't find it. Or this random example :

http://stackoverflow.com/questions/2706443/what-pitfalls-if-any-are-there-to-learning-c-c-as-a-first-language/2706531#2706531

UPDATE

I see some good answers, but not quite what I'm looking for or maybe I'm sleepy , for ex:

I have a div with id #footer , so if I type http://mysite.com#footer it will scroll me down to the footer div, how can I pass the argument which div to animate by just visiting the url http://mysite.com#footer

A: 

You would probably find everything you needed in the javascript file for the site.

Josh K
@Josh K well you are correct about that, I'd find it if I could obviously I need help where to start :D
c0mrade
Sorry, I wasn't in a position to pull through the source and find it, hence the short answer.
Josh K
+5  A: 

Take a look at the page source. Near the top there's this. I don't know much about jQuery, but it looks like a jQuery color animation for the answer element.

$(function() {

        var finalColor = '#FFF';
        $('#answer-2706531').animate({ backgroundColor:finalColor }, 2000, 
            'linear', function() {
                // shove the hex color into an element to easily compare rbg() numbers
                var test = $('<span></span>').css('background-color', finalColor);
                if ($(this).css('background-color') != test.css('background-color')) {
                    $(this).css('background-color', finalColor);
                }
            }
        );

It fades to white ('#FFF') over the course of 2000 milliseconds.

brainjam
@brainjam is this part necessary, it creates span element? `var test = $('<span></span>').css('background-color', finalColor);`
c0mrade
@c0mrade, `$(this).css('background-color')` is in a form like `rgb(255, 255, 255)`. So the most convenient and browser-independent way to compare `'#FFF'` against `rgb(255, 255, 255)` is probably to create a temporary element `test` and look at its `background-color`.
brainjam
+1  A: 

Using jQuery's Color plugin is the easiest way to animate colors:

http://plugins.jquery.com/project/color

Once that plugin is included, you can animate any colors. For example, to set an element to yellow and then slowly fade it back (over 5 seconds, say):

$("#something").css("background-color", "yellow") // set the background to yellow
               .animate({"background-color": "white"}, 5000); // fade to white

Of course, you can use hex colors instead of the color names as well (ex. "#FFFF00" instead of yellow).

Timothy Armstrong
@Timothy Armstrong it works when I call this on some event but what about by just clicking http://url#anyelement where any element can be passed and that particular element would be highlighted
c0mrade
To clarify, you want this to click on a link "url#anyelement" and have the element with the id "anyelement" to fade?In which case, simply say $("#" + window.location.hash) as your selector, and add a click() event to the element.
Timothy Armstrong
@Timothy Armstrong Great that is something I'm looking for I think
c0mrade
That was what I was looking for `$(window.location.hash) ` without #, thank you
c0mrade