Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8407777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:27:37+00:00 2026-06-09T23:27:37+00:00

I have an image sprite with 9 links. Instead of indicating the value of

  • 0

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">&nbsp;</a>
    <a class="east" href="#" alt="East">&nbsp;</a>
    <a class="south" href="#" alt="South">&nbsp;</a>
    <a class="west" href="#" alt="West">&nbsp;</a>
    <a class="northeast" href="#" alt="North East">&nbsp;</a>
    <a class="southeast" href="#" alt="South East">&nbsp;</a>
    <a class="southwest" href="#" alt="South West" >&nbsp;</a>
    <a class="northwest" href="#" alt="North West">&nbsp;</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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:27:38+00:00Added an answer on June 9, 2026 at 11:27 pm

    You’re basically doing this:

    $('.map').animate({
        top: 'eastTop',
        left: 'eastLeft'
    }, 1000, 'swing');
    

    and these are not remotely valid values. If you want to overwrite to the existing values:

    $('.map').animate({
        top: '2150',
        left: '350'
    }, 1000, 'swing');
    

    If you want to add to the existing values:

    $('.map').animate({
        top: '+=2150',
        left: '+=350'
    }, 1000, 'swing');
    

    http://api.jquery.com/animate/#animation-properties


    The problem is, I don’t have the values, because it depends on which one of the 9 links the user clicks on, that’s why I’m trying to figure out how to write a variable that grabs the value of the animation depending on the link (or the link’s class). It’s not just about writing in the values manually.

    Okay, so you want to use the value in target to identify a variable. You could do this with eval() (don’t); slightly less evil is bracket notation. Assume the variables are window-scoped, and not local:

    $(".compass a").click(function(){
        var target = $(this).attr('class');
        var destinationTop = window[target + 'Top'];
        var destinationLeft = window[target + 'Left'];
        $('.map').animate({
            top: destinationTop,
            left: destinationLeft
        }, 1000, 'swing', function(){
            //we're done!
        });
    });
    

    Better, however, to just put the values into a dictionary, and again use bracket notation:

    var values = {
        westLeft: 2150,
        westTop: 350
    };
    
    $(".compass a").click(function(){
        var target = $(this).attr('class');
        var destinationTop = values[target + 'Top'];
        var destinationLeft = values[target + 'Left'];
        $('.map').animate({
            top: destinationTop,
            left: destinationLeft
        }, 1000, 'swing', function(){
            //we're done!
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a logo link that's using a background-image (css sprite). All works fine,
I am trying to create a very basic sprite image. First off i have
I have links and a sprite image I want to render in one line
I have a sprite image for each list item (home, services and contact). I'm
I have an image sprite of sorts with multiple frames for 1 button. I'm
I have a CSS image sprite and this renderes perfect in FF, also IE.
I have a sprite image set as the background of an element on my
I have emoticons in a css sprite image that I want to display within
I have the following code a.rollover { background-image: url('sprite.jpg'); display: block; width: 191px; height:
I have an image which is made up of 2 images, a sprite. What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.