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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:23:03+00:00 2026-05-21T17:23:03+00:00

I want to position a draggable div within the divs parents center. See example

  • 0

I want to position a draggable div within the divs parents center. See example here

$("#mover").draggable({
    revert: true,
    containment: "parent"
});

<div style="width: 100px; height: 100px;" id="joystick">
    <div style="width: 10px; height: 10px" id="mover"></div><br />
</div>

Now, it works fine, but as soon as I want to position the mover at the joysticks center, the dragging doesn’t work the way I want it to.

I tried positioning it giving the mover a margin: 45px 0 0 45px;, but then (because of the box-model seeing the margin as a part of the box) the dragging still only works down and right, and not up and left.

As you see, I want to create kind of a “joystick” used to steer something. Therefore, it needs to be centered but also be movable in all axis’.

Any ideas would be highly appreciated.

  • 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-05-21T17:23:03+00:00Added an answer on May 21, 2026 at 5:23 pm

    Here’s a full working demo, you have to position the center of the joystick with position:relative;: http://jsfiddle.net/E6BQe/

    HTML:

    <div id="coords">&nbsp;</div>
    <div id="joystick">
        <div id="mover"></div>
    </div>
    

    CSS:

    #joystick { background-color:whitesmoke; width: 100px; height: 100px; padding:0px; }
    #mover { background-color:red; position:relative; margin:0px; padding:0px; width: 10px; height: 10px; left:45px; top:45px; }
    

    javascript:

    $("#mover").draggable({
        revert: true,
        containment: "parent",
        create: function(){
            $(this).data("startLeft",parseInt($(this).css("left")));
            $(this).data("startTop",parseInt($(this).css("top")));
        },
        drag: function(event,ui){
            var rel_left = ui.position.left - parseInt($(this).data("startLeft"));
            var rel_top = ui.position.top - parseInt($(this).data("startTop"));
            $('#coords').text(rel_left + ", " + rel_top);
        },
        stop: function(){
            $('#coords').html("&nbsp;");
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want the draggable object to revert back to its original position when i
I have a jquery draggable div and i want the position to be saved
Let's say I want to position an element at the coordinates x=20, y=5 within
I have a DIV container which position is absolute and I'm having a draggable
I want the draggable to be reverted to its original position if the ajax
I want to position an element in normal css relative to the element before
I want to position Dojo's Dijit Dialog relative to one of my html element.
In Autohotkey, I want to position my Gui window to right bottom of the
I want to get hold of the mouse position in a timeout callback. As
I want the best way to get mouse position for Form. I,m now set

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.