views:

57

answers:

3

Hello guys

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

A: 

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')
})
Pat
e.preventDefault(); doesn't work in IE, do you have a solution ?
john
But of course - check out my edits to see the fix.
Pat
Thank you pet, you have done a great job, Thank you very much, I was trying to get this for 2 weeks, Thanks again :) :) :)
john
A: 

Try using classNames over ids.

Also after say 10 mousedowns on a target element, that element will have 20 listeners all firing when dragging since you never remove the listeners on mouseup.

BGerrissen
This is incorrect, the mouse handlers are attached in `document.ready`, not in the `mousedown` handler, look closed at the code/parenthesis, I'll format to make it even clearer.
Nick Craver
I stand corrected, my appologies ;)Using id's instead of classnames for css expressions however limit the drag script to one single entity. Afaik, jQuery only returns the last found element with id X as per standard. Using classnames makes it more generic and predictable.
BGerrissen
A: 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; 
<html xmlns="http://www.w3.org/1999/xhtml"&gt; 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>jQuery Drag and drop</title> 
<style type="text/css"> 
#dv {
             position: absolute;
             cursor: move;
           }  
</style> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js" type="text/javascript"></script> 


<script language="javascript"> 
var x1 = y1 = 0;
var drag = false;
$(document).ready(function(e){

    //this is a possible solution
    jQuery('#dv>img').disableSelection();

    $('#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> 
</head> 
<body> 

<div id="dv" style="position:absolute;left:300px;top:200px;"> 
    <img src="http://olario.com/jquery/drupal.png" /> 
</div> 


</div> 
</body> 
</html> 
andres descalzo
this script wont work with images
john
it works, I tried it before sending
andres descalzo