Results 1 to 3 of 3

Thread: Drag and Drop JQuery

  1. #1
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default Drag and Drop JQuery

    I am currently trying to drag and drop this div box. It will move but after it resets itself. I am not sure why but it is kinda annoying. Any change anyone knows why?
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Drag Element</title>
    
    <script src="../js/jquery-1.9.js"></script>
    <script>
    $(document).ready(function() {
        var $dragging = null
    	var moveX = 0
    	var moveY = 0
    	
        $(document.body).on("mousemove", function(e) {
            if ($dragging) {
                $dragging.offset({
    				left: parseInt(e.pageX - moveX),
                    top: parseInt(e.pageY - moveY)
                });
            }
    		
        });
        
        $(document.body).on("mousedown", ".drag", function (e) {
            $dragging = $(e.target)
    		moveX = parseInt(e.pageX-$(this).parent().offset().left)
    		moveY = parseInt(e.pageY-$(this).parent().offset().top)
    		dev("Left: "+moveX+' - Top: '+moveY)
            
        });
        
        $(document.body).on("mouseup", function (e) {
    		moveX = parseInt(e.pageX-$(this).parent().offset().left)
    		moveY = parseInt(e.pageY-$(this).parent().offset().top)
    		 
            $dragging = null
    		
    		dev("Left: "+moveX+' - Top: '+moveY)
        });
    });
        function dev(s){
            $("#dev").text(s)
        }
    </script>
    
    <style>
    * {margin:0;}
    html,body { height:100%;}
    .drag { width: 200px; height: 200px; border: 1px solid black; }
    </style>
    
    
    </head>
    <body>
    
    <div class="drag">Content</div>
    <div id="dev"></div>
    
    </body>
    </html>
    Last edited by Deadweight; 04-16-2014 at 03:02 PM.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html><!-- HTML 5 supersedes XHTML -->
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Drag Element</title>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
        var $dragging = null;
    	var moveX = 0;
    	var moveY = 0;
    	var currentX, currentY; //vars to hold reference to current X/Y event position
    	
        $(document.body).on("mousemove", function(e) {
            if ($dragging) {
            	e.preventDefault(); //stop highlighting of text and images in some browsers during drag
            	var o = $dragging.offset(); //where the object is
                $dragging.offset({ //where the object is + where the mouse is - where the mouse was
    				left: o.left + e.pageX - currentX,
                    top: o.top + e.pageY - currentY
                });
    		currentX = e.pageX;//remember where the mouse pointer is now
    		currentY = e.pageY;
            }
    		
        });
        
        $(document.body).on("mousedown", ".drag", function (e) {
            $dragging = $(e.target)
            $dragging && e.preventDefault(); //stop highlighting of text and images in some browsers during drag
    		moveX = parseInt(e.pageX-$(this).parent().offset().left)
    		moveY = parseInt(e.pageY-$(this).parent().offset().top)
    		currentX = e.pageX; //remember where the mouse pointer is now
    		currentY = e.pageY;
    		dev("Left: "+moveX+' - Top: '+moveY)
            
        });
        
        $(document.body).on("mouseup", function (e) {
    		moveX = parseInt(e.pageX-$(this).parent().offset().left)
    		moveY = parseInt(e.pageY-$(this).parent().offset().top)
    		 
            $dragging = null
    		
    		dev("Left: "+moveX+' - Top: '+moveY)
        });
    });
        function dev(s){
            $("#dev").text(s)
        }
    </script>
    
    <style>
    * {margin:0;}
    html,body { height:100%;}
    .drag { width: 200px; height: 200px; border: 1px solid black; }
    </style>
    
    
    </head>
    <body>
    <div class="drag">Content</div>
    <div id="dev"></div>
    <table style="width:150%;height:150%;"><!-- HTML element to force scrollbars for the page for testing -->
    <tr>
    <td></td>
    </tr>
    </table>
    
    
    </body>
    </html>
    Last edited by jscheuer1; 04-13-2014 at 03:59 PM. Reason: minor improvements
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Actually there was a better way and here is what i did:

    Code:
    $(function(){
    	var dragging = null
    	var moveX = 0
    	var moveY = 0
    	$(document).on('mousedown','.drag',function(e){
    		dragging = e.target
    		var parentOff = $(this).offset()
    		moveX = parseInt(e.pageX - parentOff.left)
    		moveY = parseInt(e.pageY - parentOff.top)
    	})
    	
    	
    	$(document).on('mousemove',function(e){
    		if(dragging){
    			$(dragging).offset({top: (e.pageY-moveY), left: (e.pageX-moveX)})
    		}
    	})
    	
    	$(document).on('mouseup',function(){
    		if(dragging)
    			dragging = null
    	})
    	
    })
    Thanks tho

    The problem i was having was that i was actually selecting the body tag in that instance and actually moving the body and not the selected element. :/
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

Similar Threads

  1. Using a drag and drop script with a drop down menu
    By weirddemon in forum JavaScript
    Replies: 2
    Last Post: 05-12-2008, 06:31 AM
  2. Integrating Drag & Drop w/ Drop Down menu
    By weirddemon in forum JavaScript
    Replies: 3
    Last Post: 05-10-2008, 05:25 AM
  3. DOM Drag & Drop script - Flash Drag Problem
    By rudamaia in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 02-13-2008, 01:08 PM
  4. cant drag and drop
    By Johnnymushio in forum The lounge
    Replies: 7
    Last Post: 06-27-2007, 02:43 AM
  5. Drag and drop photos from drop down menu
    By swtgoddess in forum JavaScript
    Replies: 0
    Last Post: 02-28-2006, 05:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •