大家早上好,Javascript輪盤賭桌,拖放
我需要創建一個輪盤賭桌,你可以用數字(圖形)只用JavaScript/css/html拖動一個撲克籌碼。
問題1:對此最好的方法是什麼?
問題2:我想用2個循環來初始化div中的所有數字,然後使用拖放(http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx)將撲克籌碼圖像「抓拍」到最近的div。歐姆......我怎麼知道在不使用數百個if/else的情況下拍攝圖像的位置?
非常感謝!祝你今天愉快! :)
更新:
<!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>
有誰知道爲什麼ondragover/ondrop不使用此代碼工作?
您可能想要使用較新的HTML5拖放操作,而不是該文章中的內容。我認爲這給你的下降的目標元素,否則,而不是數百如果/ elses(!)你應該循環你所有的元素和工作,如果滴點是在該元素的界限內,或者哪個元素如果沒有,最接近。 – Rup 2013-04-10 00:24:54
很酷,我看着它,並試圖實現它,但ondragstart似乎並沒有與appendChild工作,雖然它與onclick工作。任何想法爲什麼?我用代碼更新了帖子。 – problemo 2013-04-10 22:16:04