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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:47:41+00:00 2026-06-05T08:47:41+00:00

How to make a element draggable without using jQuery UI? I have this code:

  • 0

How to make a element draggable without using jQuery UI?

I have this code:

<script type="text/javascript">
    function show_coords(event)
    {
        var x=event.clientX;
        var y=event.clientY;
        var drag=document.getElementById('drag');
        drag.style.left=x;
        drag.style.top=y
    }
</script>

<body style="height:100%;width:100%" onmousemove="show_coords(event)">
     <p id="drag" style="position:absolute">drag me</p>
</body>

The problem is that I want to drag while the user the pressing the mouse button. I tried onmousedown but results were negative.

  • 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-05T08:47:44+00:00Added an answer on June 5, 2026 at 8:47 am

    It will be quite easy as you get the concept.

    function enableDragging(ele) {
        var dragging = dragging || false,        //Setup a bunch of variables
            x, y, Ox, Oy,
            enableDragging.z = enableDragging.z || 1,
            current;
        ele.onmousedown = function(ev) {         //When mouse is down
            current = ev.target;
            dragging = true;                     //It is dragging time
            x = ev.clientX;                      //Get mouse X and Y and store it
            y = ev.clientY;                      // for later use.
            Ox = current.offsetLeft;             //Get element's position
            Oy = current.offsetTop;
            current.style.zIndex = ++enableDragging.z;  //z-index thing
    
            window.onmousemove = function(ev) {
                if (dragging == true) {                //when it is dragging
                    var Sx = ev.clientX - x + Ox,      //Add the difference between
                        Sy = ev.clientY - y + Oy;      // 2 mouse position to the
                    current.style.top = Sy + "px";     // element.
                    current.style.left = Sx + "px";
                    return false;                      //Don't care about this.
                }
            };
            window.onmouseup = function(ev) {
                dragging && (dragging = false);        //Mouse up, dragging done!
            }
        };
    }
    
    enableDragging(document.getElementById("drag"));  //draggable now!
    
    var ele = document.getElementsByTagName("div");
    for(var i = 0; i < ele.length; i++){              //Every div's is draggable
        enableDragging(ele[i]);                       // (only when its "position"
    }                                                 //  is set to "absolute" or
                                                      //  "relative")
    

    http://jsfiddle.net/DerekL/NWU9G/

    The reason why your code is not working is because the <div> will always follow where your cursor goes, and you are not actually dragging it. The top left corner will always follow your cursor, and this is not we wanted.


    UPDATE

    Now if you only want a grabber or something similar, just change this part of the script:

    ele.onmousedown = function(ev) {
        current = ev.target;
    

    to

    var grabber = document.createElement("div");
    grabber.setAttribute("class", "grabber");
    ele.appendChild(grabber);
    grabber.onmousedown = function(ev) {
        current = ev.target.parentNode;
    

    Now you can only click on the grabber to start the dragging process.

    http://jsfiddle.net/DerekL/NWU9G/7/

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

Sidebar

Related Questions

Hay I'm using this code to clone a draggable element $('.draggable').draggable({helper: clone}); $('.draggable').bind('dragstop', function(event,
How do I make an element, e.g. a div, draggable using jQuery?
I am attempting to make an element shake upon hover without using jQuery UI
I'm using the jQuery .scroll() function to make a certain element fade to 0.2
How can i make element both draggable and resizable in jquery?
I found this function to make my divs draggable and it works fine. right
I am using JavaScript and trying to make a skew effect on a div.
I've wrote some jquery code with some draggable elements and one droparea. Unfortunately my
What would be the best way to make an element draggable when clicking anywhere
I have a draggable connected to a sortable: $(#panelTarget).sortable({ distance: '15' }); var element

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.