我在jsfiddle上有一个代码,我无法让它工作,除了可拖动之外,我的所有功能都在工作。当我添加$(init)
时,交易按钮停止工作,我无法让它处理卡片,一旦我删除它$(init)
它可以工作,但不会拖动。有人可以帮我弄明白如何制作可拖动的工作吗?谢谢。
我的JsFiddle: http://jsfiddle.net/otpspbhs/
$('#deal').click(function () {
dealCard(randomCard());
});
function init() {
$('.drop').droppable( {
drop: handleDropEvent
});
}
var cardsInDeck = new Array();
var numberOfCardsInDeck = 5;
cardsInDeck[0] = "AceHearts";
cardsInDeck[1] = "Clubs2";
cardsInDeck[2] = "ClubsAce";
cardsInDeck[3] = "DiamondsKing";
cardsInDeck[4] = "SpadesJack";
function dealCard(i) {
if (numberOfCardsInDeck == 0) return false;
var img = document.createElement("img");
img.src = "https://cop4813eaglin.pbworks.com/f/" + cardsInDeck[i] + ".png";
document.body.appendChild(img);
removeCard(i);
}
function randomCard() {
return Math.floor(Math.random() * numberOfCardsInDeck);
}
function handleDropEvent( event, ui ) {
var draggable = ui.draggable;
$('#drop').html( 'The card with ID "' + draggable.attr('id') + '" was dropped onto me!' );
}
function removeCard(c)
{
// simply make every higher numbered card move down 1
for (j=c; j <= numberOfCardsInDeck - 2; j++)
{
cardsInDeck[j] = cardsInDeck[j+1];
}
numberOfCardsInDeck--;
}
.drop {
float: left;
width: 400px;
height: 200px;
background-color: #BC5347;
}
<input type="button" value="Deal Card" id="deal" />
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<div id='drop' class='drop'>Drop Here</div>
答案 0 :(得分:1)
首先,包括jquery ui。
其次,在创建图像后使图像可拖动:
function dealCard(i) {
if (numberOfCardsInDeck == 0) return false;
var img = document.createElement("img");
img.src = "https://cop4813eaglin.pbworks.com/f/" + cardsInDeck[i] + ".png";
$(img).draggable();
document.body.appendChild(img);
removeCard(i);
}
此处已更新fiddle。
答案 1 :(得分:0)
请参阅Drag and Drop Basic tutorial
要使img拖延,你必须:
答案 2 :(得分:0)
简单的逻辑,使卡可以拖动,你的盒子可以放置。
并添加更多...使用.append
,以便拖动的卡片将被删除并仅添加到您的框内