I have an image sprite with 9 links. Instead of indicating the value of each link, I’m trying to animate in jQuery, after dynamically finding out the value and moving accordingly.
Here’s the markup:
<div class="compass">
<a class="north" href="#" alt="North"> </a>
<a class="east" href="#" alt="East"> </a>
<a class="south" href="#" alt="South"> </a>
<a class="west" href="#" alt="West"> </a>
<a class="northeast" href="#" alt="North East"> </a>
<a class="southeast" href="#" alt="South East"> </a>
<a class="southwest" href="#" alt="South West" > </a>
<a class="northwest" href="#" alt="North West"> </a>
</div>
And the jQuery:
var westLeft = +2150;
var westTop = +350;
$(".compass a").click(function(){
var target = $(this).attr('class');
var destinationTop = target + 'Top';
var destinationLeft = target + 'Left';
$('.map').animate({
top: destinationTop,
left: destinationLeft
}, 1000, 'swing', function(){
//we're done!
});
});
So I got destinationTop to dynamically show westTop when I click on the west link, but NOT the actual value of westTop. Same for destinationLeft.
Thank you very much.
You’re basically doing this:
and these are not remotely valid values. If you want to overwrite to the existing values:
If you want to add to the existing values:
http://api.jquery.com/animate/#animation-properties
Okay, so you want to use the value in
targetto identify a variable. You could do this witheval()(don’t); slightly less evil is bracket notation. Assume the variables are window-scoped, and not local:Better, however, to just put the values into a dictionary, and again use bracket notation: