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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:03:25+00:00 2026-06-18T00:03:25+00:00

below code try to make img be draggable(regarding this ), it works but I

  • 0

below code try to make img be draggable(regarding this), it works but I can’t figure how to make if img move all wrap drag_wp move together. http://jsfiddle.net/cAeKG/8/ any suggestion?

js

function enableDraggin(el){
    var dragging = dragging || false, x, y, ox, oy, current;
    el.onmousedown = function(e){
        e.preventDefault();
        current = e.target;
        dragging = true;
        x = e.clientX;
        y = e.clientY;
        ox = current.offsetLeft;
        oy = current.offsetTop;
        window.onmousemove = function(e) {
            if (dragging == true) {
                var sx = e.clientX - x + ox,
                sy = e.clientY - y + oy;
                current.style.left = sx + "px";
                current.style.top = sy + "px";
                return false;
            }
        };
        window.onmouseup = function(e) {
            dragging && (dragging = false);
        };
    };
};
var el = $('.drag_wp');
for(var i = 0; i < el.length; i++){
    enableDragging(el[i]);
};

html & css

<div class="drag_wp">
    <img src="..." class="drag_el">
    <div></div>
    ....// other div
</div>
.drag_wp{
    position: absolute;
    width: auto;
    height: auto;
    background:red;
}
.drag_el{
    position: absolute;
    width: 200px;
    height: auto;
}
  • 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-18T00:03:26+00:00Added an answer on June 18, 2026 at 12:03 am
    function enableDraggin(el){
        var dragging = dragging || false, x, y, ox, oy, current;
        el.onmousedown = function(e){
            e.preventDefault();
            current = e.target;
            //just add this
            if(!$(current).hasClass(".drag_wp"))
                 current = $(current).closest(".drag_wp")[0];
            //just add this
            dragging = true;
            x = e.clientX;
            y = e.clientY;
            ox = current.offsetLeft;
            oy = current.offsetTop;
            window.onmousemove = function(e) {
                if (dragging == true) {
                    var sx = e.clientX - x + ox,
                    sy = e.clientY - y + oy;
                    current.style.left = sx + "px";
                    current.style.top = sy + "px";
                    return false;
                }
            };
            window.onmouseup = function(e) {
                dragging && (dragging = false);
            };
        };
    };
    

    e.target returns element that was clicked (image in most cases for your HTML). If it has no class .drag_wp, just find closest parent with that class and use it as current.

    Demo: http://jsfiddle.net/fTeXL/

    Also, as you are already using jquery, you may find it better to use corresponding plugin from jQuery UI: http://jqueryui.com/draggable/

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

Sidebar

Related Questions

below code try to make if btn mousedown find parent siblings(img_wp) img, but it
In the below code how can i make it load the file into a
The below code and the config works fine, but force to enter user name/password
I have rewritten this to try and make it more descriptive with more code
I made a kernel module and used the code below to try to make
Below code is my PHP CLi TCP server socket , but I try to
I've done below code to try getting all contacts phone number from address book:
I have the below code. I can't make it work. I have to mention
i use below code - Just try again. - to prevent deadlock. it seems
the code below gives compilation error when I try to create test t[2]; because

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.