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..
|