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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:02:24+00:00 2026-05-16T01:02:24+00:00

I have created a jQuery script to drag elements over the document. I done

  • 0

I have created a jQuery script to drag elements over the document. I done the script and is working perfectly. but when I add an image the script fails. Why I cant drag an image over my document, I can drag other elements such as div, span etc . I am not interested to use jQuery UI for this purpose

my code follows

<script language="javascript" src="jquery-1.4.2.min.js"></script>
<script language="javascript">
  var x1 = y1 = 0;
  var drag = false;
  $(document).ready(function(e){
    $('#dv').mousedown(function(e){ 
      x1 = e.pageX - parseInt($('#dv').css('left'));
      y1 = e.pageY - parseInt($('#dv').css('top'));
      drag = true; 
    })
    $(document).mouseup(function(e){ drag = false; })
    $(document).mousemove(function(e){ 
      if(!drag) return
      $('#dv').css('left',eval(e.pageX - x1)+'px')
      $('#dv').css('top',eval(e.pageY - y1)+'px')
    })
  });
</script>

please check this olario.com/jquery/first.php ( a normal div with text) working perfectly

second olario.com/jquery/second.php, that doesn’t work with image

I spent two weeks to fix this issue, hope this forum will help me

Thanks a lot in advance

  • 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-16T01:02:25+00:00Added an answer on May 16, 2026 at 1:02 am

    You can get it working with a tiny tweak in your mousedown event handler:

    $('#dv').mousedown(function(e){ 
    
        e.preventDefault();
    
        x1 = e.pageX - parseInt($('#dv').css('left'));
        y1 = e.pageY - parseInt($('#dv').css('top'));
        drag = true; 
    
    });
    

    The preventDefault() call stops the browser from trying to natively move the image itself and lets your script take over.

    Edit

    To get this working in IE, you’ll also have to add a preventDefault() call to your mousemove event handler:

    $(document).mousemove(function(e){ 
        if(!drag) return;
    
        e.preventDefault();
        $('#dv').css('left',eval(e.pageX - x1)+'px')
        $('#dv').css('top',eval(e.pageY - y1)+'px')
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created jquery image slider and it is working good. However i have
i have this jquery script: $('#add').click(function(){ $('<div class=drag style=left:20px;/>') .text( num++ ) .appendTo( document.body
I have created the jQuery Script below, But am having a few problems converting
I have created a simple JQuery script with hovering effect on some links. The
I have created a dynamic list picker script using Jquery 1.3 and PHP that
I'm a jQuery newbie, and I have trouble with a little script I created.
Suppose I have created one jQuery function to check if elements exist or not
I have created a dynamic dependent menu using a jquery script i found on
I have created a script which is surprisingly working. (I do not do much
I have a drag and drop script using an older version of jquery which

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.