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 7750903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:24:41+00:00 2026-06-01T11:24:41+00:00

I am using JQuery and would like to do the following. I have 2

  • 0

I am using JQuery and would like to do the following. I have 2 images of a car. One that faces left and one that faces right (x-axis). When a user clicks and drags the car in a direction I want the image
of the car to switch to be the one that faces in the same direction.

Here’s what I have so far:

I have a DIV called #car that is assigned to this code

$(function() {
    $( "#car" ).draggable();
  });

and it drags just fine. I then have an image that is wrapped in the #car div called #myImage and the HTML code is as follows:

 <img id="myImage" src="" alt="" height="42" width="42" />

I then have the Jquery code that I am attempting to “connect” to #myImage below.

function carDirection(){
var currentPosition = $("#myImage").text(this.id + offset.left );
if ("#myImage" <= currentPosition){
    $("#myImage").attr("src", "leftCar.png");   
  }
  else {
    $("#myImage").attr("src", "rightCar.png");
  }
};

So to summarize and make this even clearer I am creating a div that is using JQuery’s draggable effect and I simply want an image that is wrapped in it to change depending on if the user drags it left or right with the mouse. The image should stay the same after the user lets go of the mouse.

Thank you.

  • 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-01T11:24:42+00:00Added an answer on June 1, 2026 at 11:24 am

    You need to use the drag callback for .draggable() to determine if the car has moved left or right since last time.

    EDIT: Code updated to be a lot clearer with more comments.

    jsFiddle example is available.

    // defining custom object to hold images to use. 
    // Change as required in actual code.
    var sprites = {
        car: {
            left: 'http://placehold.it/200&text=Left',
            right: 'http://placehold.it/200&text=Right'
        }
    };
    
    $(function() {
        var carImage = document.getElementById('myImage'); // no need to use jQuery to get by ID
        $('#car').draggable({
            // define the behaviour to be run continually
            // whilst car is being dragged.
            drag: function(event, ui) {
                // get the last left position (think x co-ordinate)
                // of the thing you are dragging (the helper) 
                // using the jQuery data method, which stores
                // that info against the actual HTML element itself.
                // Note that if this is the first time the function is
                // run, there IS no such data, so the "|| thisX" ensures 
                // that there is a value the first time.
                var thisX = ui.offset.left,
                    lastX = ui.helper.data('lastLeft') || thisX;
                if (thisX < lastX) {
                    // must be moving left
                    carImage.src = sprites.car.left;
                } 
                if (thisX > lastX) {
                    // must be moving right
                    carImage.src = sprites.car.right;
                }
                // don't do anything if the "x-coord" is the same
                // or else it causes flickering.
    
                // store the current position to compare with next time
                // this function is run.
                ui.helper.data('lastLeft', thisX);
            }
        });
    });​
    

    Note that you would be better to use CSS classes and a sprite image (a single image containing both left and right car images), as I believe changing the background position of the same image is faster for most browsers than changing the src. Also, you only need to load one image, and so you don’t need to load the image for the other direction the first time it changes. Does that make sense?

    Is there anything else from my code that doesn’t make sense now?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jquery mobile. I would like to navigate from one page to
I am using jQuery and would like to display the images from my PC,
I need to have images that extend along the left and right sides of
I have the following DOM structure (unordered list), that I would like to sort
I would like to have a function that returns the repsonseText of a jQuery.ajax()
I am using jQuery datepicker, I have two datepicker input fields, like following: <html>
I have an array of numerical values and, using jQuery, would like to preload
I would like to implement the following feature using jQuery scrollTo function or some
I am new to using jquery and would like to know how to append
I'm using jQuery Validate and would like to re-validate a group of fields whenever

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.