animate to coordinates instead of amount of distance
Is there a way to animate/slide a div to a particular set of x/y or just x coordinates?
left:100 is moving by 100, not to X=1开发者_JAVA技巧00.
I think anchors would be cool but I can't seem to figure how to say"move this div to x=100 and then later, "move this div to where x=-200" for instance.
youll have to calculate where you want to move it to... something like
$(selector).animate({
'left': $(selector).position().left-200
}, 500);
Ah. This is actually a CSS problem. Your element should just use position: absolute
. When positioning your element absolutely, properties like left
and top
are relative to the origin (upper-left corner) of the nearest ancestor that has a non-static position (i.e. uses something other than the default position: static
).
Of course, absolutely positioning your element has other consequences, such as removing it from the normal flow of content, and having margins that no longer collapse. If you need to use relative positioning, you will need to leverage jQuery.position()
or jQuery.offset()
, and do some math to move the element to the desired location.
精彩评论