Jump to content

random image drag and drop


zaneosak

Recommended Posts

Hello,

 

I was curious if anybody could help me with making this code I wrote today choose a random image out of 3 that I have (image1.jpg, image2.jpg and image3.jpg) instead of just the image I have on the screen whenever I click to add an image.

 

Currently I am just using 1 image and would like to make it choose 1 of 3 possible choices when I click to add. Also if anybody has any idea why the images arent adding at the spot of the cursor when I click I would be interested in hearing, but I know I need an array but do not know how to get the src of the images randomly.

 

Thanks if anybody can help

 

Code is below:

 

<html>
<head>
<style>
<!--
.dragme{position:relative;}
-->
</style>
<script>


var ie=document.all;
var nn6=document.getElementById&&!document.all;

var isdrag=false;
var x,y;
var dobj;

function movemouse(e)
{
  if (isdrag)
  {
    dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
    dobj.style.top  = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
    return false;
  }
}

function selectmouse(e) 
{
  var fobj       = nn6 ? e.target : event.srcElement;
  var topelement = nn6 ? "HTML" : "BODY";

  while (fobj.tagName != topelement && fobj.className != "dragme")
  {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }

  if (fobj.className=="dragme")
  {
    isdrag = true;
    dobj = fobj;
    tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
    document.onmousemove=movemouse;
    return false;
  }
}

document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");

</script>


<style>
img{position:absolute}
</style>
<script>
document.onclick=add


function add(e){
if (window.event){
	e=window.event
	t=e.srcElement
}
else{
	t=e.target
}
if (t.nodeName=="IMG"){
	if(e.type=="mouseover")
		t.style.border="2px solid red"
	else if(e.type=="mouseout")
		t.style.border="0px "
}
else{
	var H=document.getElementById("img1")
	var C=H.cloneNode("true")

	C.style.left=e.clientX
	C.style.top=e.clientY

	document.body.appendChild(C)

}

}
function tellus(){


}

</script></head><body>
Click to add an image, Click Image to drag<br><br>
<img style="border: 0px none ;" src="image1.jpg" onmouseover="add(event)" onmouseout="add(event)" 

ondragstart="tellus()" id="img1" alt="" border="0" class="dragme">


</body></html>

Link to comment
https://forums.phpfreaks.com/topic/145619-random-image-drag-and-drop/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.