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

  • Home
  • SEARCH
  • 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 840263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:35:44+00:00 2026-05-15T05:35:44+00:00

What would be the best way to make an element draggable when clicking anywhere

  • 0

What would be the best way to make an element draggable when clicking anywhere in the window?

Would I have to use a container <div> that covers the whole window?

I tried making the body draggable but that didn’t work.

The problem with using a container <div> is that it moves and therefore doesn’t cover the whole of the screen any more after it has been dragged.

What about making a really vast container <div> that spans a large number of pixels in every direction so you would never get to the edge of it. Is that a bad idea?

The idea (simplified) is to have a page with a square in the middle that can be dragged by dragging any part of the window.

Here’s a wonderfully unnecessary mockup 🙂

alt text

I’m trying with a full screen div, but when I reset it, the element within it moves back with it.. http://jsfiddle.net/LUFf6/

  • 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-15T05:35:45+00:00Added an answer on May 15, 2026 at 5:35 am

    EDIT: revised to include mouse offset from the div.

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
    <script>
    function doMove(e) {
      var $d = $('#myDiv');
      var off = $d.offset();
      var left = e.pageX + $d.data('left');
      var top = e.pageY + $d.data('top');
      $d.offset({left: left, top: top});
    };
    $(function() {
      $(document).mousedown(function(e) {
         var $d = $('#myDiv');
         var off = $d.offset();
         $d.data('left',off.left - e.pageX);
         $d.data('top',off.top - e.pageY);     
         $(document).bind('mousemove', doMove);
      }).mouseup(function(e) {
         $(document).unbind('mousemove',doMove);
      });
    });
    </script>
    <style>
    div#myDiv {
      width: 50px;
      height: 50px;
      background-color: red;
      position: absolute;
      top: 100px;
      left: 100px;
    }
    </style>
    <div id="myDiv"></div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder what the best way to make an entire tr clickable would be?
I want to make a code snippet database web application. Would the best way
In Java, what would the best way be to have a constantly listening port
What would be the best way to have a list of items with a
What's the best way to make an element unselectable using jQuery? I know you
I need to make it so that an element which would normally (depending on
I'm using jQuery, what would be the best way to get every element with
What would be the best way to accomplish something like this? Suppose I have
What would be the best way to optimize this code below to make it
What would be the best way to fill a C# struct from a byte[]

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.