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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:08:55+00:00 2026-06-13T14:08:55+00:00

I want to create a movable/draggable div in native javascript without using jquery and

  • 0

I want to create a movable/draggable div in native javascript without using jquery and libraries. Is there a tutorial or anythign?

  • 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-13T14:08:56+00:00Added an answer on June 13, 2026 at 2:08 pm

    OK, here’s my personal code that I use for lightweight deployments (projects where using a library is either not allowed or overkill for some reason). First thing first, I always use this convenience function so that I can pass either an id or the actual dom element:

    function get (el) {
      if (typeof el == 'string') return document.getElementById(el);
      return el;
    }
    

    As a bonus, get() is shorter to type than document.getElementById() and my code ends up shorter.

    Second realize that what most libraries are doing is cross-browser compatibility. If all browsers behave the same the code is fairly trivial. So lets write some cross-browser functions to get mouse position:

    function mouseX (e) {
      if (e.pageX) {
        return e.pageX;
      }
      if (e.clientX) {
        return e.clientX + (document.documentElement.scrollLeft ?
          document.documentElement.scrollLeft :
          document.body.scrollLeft);
      }
      return null;
    }
    
    function mouseY (e) {
      if (e.pageY) {
        return e.pageY;
      }
      if (e.clientY) {
        return e.clientY + (document.documentElement.scrollTop ?
          document.documentElement.scrollTop :
          document.body.scrollTop);
      }
      return null;
    }
    

    OK, the two functions above are identical. There’re certainly better ways to write them but I’m keeping it (relatively) simple for now.

    Now we can write the drag and drop code. The thing I like about this code is that everything’s captured in a single closure so there are no global variables or helper functions littering the browser. Also, the code separates the drag handle from the object being dragged. This is useful for creating dialog boxes etc. But if not needed, you can always assign them the same object. Anyway, here’s the code:

    function dragable (clickEl,dragEl) {
      var p = get(clickEl);
      var t = get(dragEl);
      var drag = false;
      offsetX = 0;
      offsetY = 0;
      var mousemoveTemp = null;
    
      if (t) {
        var move = function (x,y) {
          t.style.left = (parseInt(t.style.left)+x) + "px";
          t.style.top  = (parseInt(t.style.top) +y) + "px";
        }
        var mouseMoveHandler = function (e) {
          e = e || window.event;
    
          if(!drag){return true};
    
          var x = mouseX(e);
          var y = mouseY(e);
          if (x != offsetX || y != offsetY) {
            move(x-offsetX,y-offsetY);
            offsetX = x;
            offsetY = y;
          }
          return false;
        }
        var start_drag = function (e) {
          e = e || window.event;
    
          offsetX=mouseX(e);
          offsetY=mouseY(e);
          drag=true; // basically we're using this to detect dragging
    
          // save any previous mousemove event handler:
          if (document.body.onmousemove) {
            mousemoveTemp = document.body.onmousemove;
          }
          document.body.onmousemove = mouseMoveHandler;
          return false;
        }
        var stop_drag = function () {
          drag=false;      
    
          // restore previous mousemove event handler if necessary:
          if (mousemoveTemp) {
            document.body.onmousemove = mousemoveTemp;
            mousemoveTemp = null;
          }
          return false;
        }
        p.onmousedown = start_drag;
        p.onmouseup = stop_drag;
      }
    }
    

    There is a reason for the slightly convoluted offsetX/offsetY calculations. If you notice, it’s just taking the difference between mouse positions and adding them back to the position of the div being dragged. Why not just use the mouse positions? Well, if you do that the div will jump to the mouse pointer when you click on it. Which is a behavior I did not want.

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

Sidebar

Related Questions

I want create listview without layout_height. I mean if there is 100 item in
I want create a plugin in jquery. this is my html code : <div
I want to create a switching button with movable action like if it's showing
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want create a pdf using iText. The method which does this is a
i'm using google maps v3 and i want create a new map associate to
I want create a new JavaScript object based on an existing JavaScript object. Till
I want create own SetupTypeDlg using wix default dialogs to show the disk costs
I want create a site by command line using appcmd. How can I associate
I am trying to create a Javascript/Jquery based application that will eventually go into

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.