Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Need help with javascript drag and drop of an image
Old 03-09-2009, 01:37 AM Need help with javascript drag and drop of an image
Novice Talker

Posts: 8
Trades: 0
This is what my script looks like:

PHP Code:
var isGrabbed null;

function 
coordX(){
    if(
document.onmousemove.pageX){
        return 
document.onmousemove.pageX;
    }
    return 
document.onmousemove.clientX;
}

function 
coordY(){
    if(
document.onmousemove.pageY){
        return 
document.onmousemove.pageY;
    }
    return 
document.onmousemove.clientY;
}

function 
grabImg() {
    
isGrabbed "yes";
}

function 
dragImg() {
    if (
isGrabbed == "yes") {
        
document.getElementById("wu").style.left=coordX()-25+"px";
        
document.getElementById("wu").style.top=coordY()-25+"px";
    } else {
        
setTimeout("dragImg()"25);
    }
}

function 
dropImg() {
    
isGrabbed null;

and this is what the html looks like:

HTML Code:
<img src="wutang.jpg" alt="Drag me!" title="Drag me!" id="wu" onmousedown="grabImg()" onmouseup="dropImg()" onmousemove="dragImg()" />
but it doesn't work, and I have tried at least ten different things, but none seem to work. What am I missing? Thanks to everyone, I'm very frustrated right now.
mnemonik is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-09-2009, 06:34 AM Re: Need help with javascript drag and drop of an image
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
I suggest downloading jQuery and using that instead.
It's absolutely brilliant.

If you don't want to do that but want to stick with your old code, I suggest you put in some alerts at certain places to check if the variables you are using are working properly.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 03-15-2009, 08:14 PM Re: Need help with javascript drag and drop of an image
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
I'll post some code here when I get a chance.

-Moatist
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>
moatist is offline
Reply With Quote
View Public Profile
 
Old 03-15-2009, 10:06 PM Re: Need help with javascript drag and drop of an image
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
Try this:

(paste into head):

Code:
<script type="text/javascript"> 
	//initiate variables
	var element;
	var DragNow = false;
	var dx=0;
	var dy=0;
	
	function initDrag(e)
	{	
		e = (e)? e : event;
		
		DragNow = true;
		
		//
		dx = Math.abs(parseInt(element.style.left) - e.clientX);
		dy = Math.abs(parseInt(element.style.top) - e.clientY);
		
		//declare mousemove and mouseup event
		document.onmousemove = startDrag;
		document.onmouseup = stopDrag;
	}
 
	function stopDrag()
	{
		DragNow = false;
	}
	
	function startDrag(e)
	{
		e = (e)? e : event;
		if(DragNow)
		{
			element.style.top = (e.clientY - dy) + 'px';
			element.style.left = (e.clientX - dx) + 'px';
		}
	}
	
	window.onload = function()
	{
	  //set the draggable element
	  element = document.getElementById("ball"); //i.e: document.getElementById("pic")
	  
	  //initiate mousedown event
	  element.onmousedown = initDrag;
	}
</script>
(paste into body):
Code:
<div id="pic" style="left:0;top:0;position:absolute;width:306px;height:174px;background-image:url('image.jpg');"> </div>
The code is not perfect, but it is cross-browser compliment.

You may have some trouble, depending on your needs, with the div starting out in the top-left corner. This can be modified but absolute-positioning is necessary.

-Moatist
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>

Last edited by moatist; 03-15-2009 at 10:07 PM..
moatist is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need help with javascript drag and drop of an image
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29003 seconds with 12 queries