If I understand correctly, you want the box to be red until someone hovers, then turn gray on hover, and then become black instead of red.
I prefer to let CSS do as much of the work as possible, since it often processes faster and is easier to work with, especially for something so simple. CSS animations can replace the jQuery animate function in most cases. jQuery is needed only to set a flag that the box has been hovered, and then isn't really needed after that.
jQuery:
I use the updated "on" function for the mouseover, so you can easily turn it off when it's not needed. This basically sets a class on the box the first time it's hovered.
$(document).ready(function () {
$('.spring').on('mouseover', function () {
$('.spring')
.addClass('touched')
.off('mouseover');
});
});
CSS:
I find that animations are smoother and allow finer control with CSS transitions rather than jQuery animate. We set the hover state to gray, and the .hovered class to black, and everything's right there in the CSS.
.spring {
background: red;
width: 100px;
height: 30px;
transition: background 0.5s ease
}
.spring:hover, .spring.touched:hover {
background: #ccc;
}
.spring.touched {
background: #000;
}
This gives you easy control over every aspect of the appearance, without having to add cumbersome css code into the jQuery.
See the Fiddle here
Only one gotcha—before you use it, check the table for CSS animation support:
http://caniuse.com/css-animation . I usually consider transitions a "nice to have" item rather than an essential, so I'm not too worried that IE9 and below won't see the transitions. They still get the color changes just fine, so there's really no functionality lost. The advantage of not having all that junk in the javascript is worth it.