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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:40:40+00:00 2026-05-20T09:40:40+00:00

So, what it does: You can successfully drag something anywhere, but when you click

  • 0

So, what it does: You can successfully drag something anywhere, but when you click it again, it resets to its original position (I don’t know why), and when you try to drag it again, as soon as your cursor touches the object it disappears (I don’t know why). I’m hoping someone can tell me why it is happening and how to fix it!

// JavaScript Document

var posX;
var posY;
var element;

function drag() {

element = document.getElementById("square");
posX = event.clientX;
posY = event.clientY;

element.addEventListener("mousemove", move, false);
}

function move() {

    element.addEventListener("mouseup", drop, false);

    element.style.left = event.clientX - posX + "px";
    element.style.top = event.clientY - posY + "px";
}

function drop() {

    element.removeEventListener("mousemove", move, false);
    element.removeEventListener("mouseup", drop, false);
}

the html is a simple (position is set to relative):

<p id="square" onmousedown="drag(event)">meep</p>

Lastly, and most importantly, thank you all for your time =]

  • 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-20T09:40:41+00:00Added an answer on May 20, 2026 at 9:40 am
    posX = event.clientX;
    posY = event.clientY;
    

    The problem is these values are always set to the offset of the click from the top of the body, NOT from the top of the square element. The first time they worked fine, because the offset from the top of the body was the same thing as that of the element, because its position was right up against the body. Once you moved it, it would have the offset from the side of the body from wherever you just moved it to. When you dragged it with the mouse, it would move it with the mouse’s position shifted over the number of pixels you just moved it to. This often moved it completely off the screen. You want the element to move with the mouse where the top of the element stays in the same position relative to the mouse.

    If this doesn’t make sense, post a comment and I’ll try to make it a little clearer. 😀

    This worked for me:

    <html>
    <head>
    <script type="text/javascript">
    // JavaScript Document
    
    var posX;
    var posY;
    var element;
    
    function drag(event) {                //added event parameter
    
    element = document.getElementById("square");
    posX = event.clientX - parseInt(  element.offsetLeft );  //this is the big change!
    posY = event.clientY - parseInt(  element.offsetTop );
    
    element.addEventListener("mousemove", move, false);
    }
    
    function move(event) {                  //added event parameter
        if (typeof(document.getElementById("square").mouseup) == "undefined") {  //this just prevents redundantly adding the same event handler repeatedly, even though duplicates are removed anyway
            element.addEventListener("mouseup", drop, false);
        }
    
        element.style.left = event.clientX - posX + "px";
        element.style.top = event.clientY - posY + "px";
    }
    
    function drop() {
    
        element.removeEventListener("mousemove", move, false);
        element.removeEventListener("mouseup", drop, false);
    }
    </script>
    <style type="text/css">
    body {
     width: 1000px;
     height: 1000px;
     background-color: blue;
     }
    
    p {
     position: relative; 
     background-color: red;
     width: 100px;
     height: 100px;
     left: 1px;
     top: 1px;
    }
    
    </style>
    <body>
    <p id="square" onmousedown="drag(event)">meep</p>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know where I can find a library of common but difficult (out
Does anyone know how I can, in platform-independent C++ code prevent an object from
Does anyone know how I can check to see if a directory is writeable
Does anyone know how I can achieve the following effect in OpenGL: Change the
Does anyone know how I can get rid of the following assembler warning? Code
Does any one know how can I convert a BSTR to an int in
Does anyone know how I can get a format string to use bankers rounding
Does anyone know of a way I can code-profile my Windows Sidebar Gadget? I've
I am building a simple site with extJS. I can successfully attach click events
MFC programs can't normally write to stdout. MFC does something weird with the stdout/stdin

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.