How do I say, slide an element a few pixels to the side from its initial position using jQuery? I know there is an animate() function but I have no idea how to use it.
Tips, tutorials would be appreciated.
EDIT:
I am now trying to change the background color of an element on mouseover (I would also like to swap background images eventually, but that’s not for now…)
I’m currently using the following code. what’s wrong? (.link is a class referring to a bunch of a elements)
//light up links on mouseover
$(".link").mouseover(function(event){
$(this).animate({'color' : '#000099'}, "fast");
});
//dim link on mouseout
$(".link").mouseout(function(event){
$(this).animate({'color' : '#efefef'}, "fast");
});
First, read the documentation for
animate.Then, you must define the element you are trying to move to have its
positionattribute set torelativeorabsolutevia CSS or jQuery.If you set it to
relative, you can just supply the amount of pixels you want to move it with:If you chose absolute positioning, you must first work out the starting position of the element using
.offset().leftand add the desired amount of pixels to that, then animate to that position. For example:This works if the #element’s parent elements are statically positioned (otherwise the offset().left doesn’t match with the absolute left value).