此代码适用于拖放游戏。目前,警报(“正确”)在每个不同的部分被拖动到其目标之后出现。如何更改此设置以便警报仅在游戏结束时出现一次,说明有多少是正确的?
html:
<img class="drag-me" id="drag1" src="images/drag1.png">
<img class="drag-me" id="drag2" src="images/drag2.png">
<img class="drag-me" id="drag3" src="images/drag3.png">
<img class="drag-me" id="drag4" src="images/drag4.png">
<img class="target" id="target1" src="images/target1.png">
<img class="target" id="target2" src="images/target2.png">
<img class="target" id="target3" src="images/target3.png">
<img class="target" id="target4" src="images/target4.png">
jquery:
$('.drag-me').draggable({revert:"invalid", snap:".target"});
$('.target').droppable({
drop: function( event, ui ) {
itemsInPosition++;
// get id of draggable and droppable id
var draggableID = $(ui.draggable).attr("id");
var droppableID = $(this).attr("id");
if(draggableID[4]==droppableID[6]){
alert("correct")
testIfComplete();
totalCorrect++;
}
}
});
var itemsInPosition = 0;
var totalItems = 4;
var totalCorrect = 0;
function testIfComplete(){
if(itemsInPosition==totalItems){
alert("You got " + totalCorrect + " right")
}
}
谢谢!
答案 0 :(得分:0)
可能是,您应该选择合适的库:jquery UI的JS + CSS。
请参阅此小提琴。有用。 http://jsfiddle.net/abdennour/67SrS/
var itemsInPosition = 0;
var totalItems = 4;
var totalCorrect = 0;
$('.drag-me').draggable({revert:"invalid", snap:".target"});
$('.target').droppable({
drop: function( event, ui ) {
itemsInPosition++;
// get id of draggable and droppable id
var draggableID = $(ui.draggable).attr("id");
var droppableID = $(this).attr("id");
if(draggableID[4]==droppableID[6]){
alert("correct")
testIfComplete();
totalCorrect++;
}
}
});
你可以得出结论&amp;从这个小提琴下载合适的库:
http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/overcast/jquery-ui.css