Good morning everyone,
I need to create a roulette table where you can drag a poker chip on the number(graphically) using only javascript/css/html.
Question 1: What would be the best approach for this?
Question 2: I thought of using 2 for loops to initialize divs with all the numbers in them and then use drag and drop(http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx) to "snap" the poker chip image to the closest div when it is released. Euhm... How would I know where to snap the image without using hundreds of if/else?
Thank you very much! Have a nice day! :)
Update:
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
#div1 {
width: 20px;
height: 40px;
padding: 10px;
border: 1px solid #aaaaaa;
}
#game {width:350px;height:70px;padding:10px;border:1px solid #aaaaaa;}
</style>
</head>
<body>
<p>Drag the W3Schools image into the rectangle:</p>
<br>
<div id="game">
</div>
<img id="drag1" src="images/c.png" draggable="true" ondragstart="drag(event)" width="336" height="69">
<script>
function allowDrop(ev)
{
ev.preventDefault();
}
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
function dragAlert() {
alert("Ondrag");
}
var numberCount = 1;
var divId = 0;
var div = document.createElement("div");
div.className = "div1";
/*divId = "id";
divId += numberCount;*/
div.id = "div1";
div.ondrop = function() {
drop(event);
};
div.ondragover = function() {
allowDrop(event);
};
document.getElementById("game").appendChild(div);
numberCount++;
</script>
</body>
</html>
Does anyone know why ondragover/ondrop doesn't work with this code?