** EDIT **
我目前正在创建一个拖放拼写游戏,用户将字母拖到突出显示的单词上以进行拼写并显示后面的图像。
当单词以“.spellword”样式突出显示时,它指示用户拼写该单词。当用户将字母拖到该区域时,他/她可以将字母拖到3个字母空间中的任何位置,但是我需要将它们从“左”拖放到“右”,以确保单词拼写正确。
基本上,当一个字母被放到单词上时,我需要将其对齐到左侧(单词的第一个字母),然后下一个字母放到该单词的下一个字母上,依此类推...所以它的拼写顺序正确
我该怎么做才能确保这一点?
可拖放的脚本是...
$('.drag').draggable({
helper: 'clone',
snap: '.drop',
grid: [62, 62],
revert: 'invalid',
snapMode: 'corner',
start: function(){
var validDrop = $('.drop-box.spellword');
validDrop.addClass('drop');
makeDroppables();
}
});
function makeDroppables(){
$('.drop').droppable({
drop: function(event, ui) {
word = $(this).data('word');
guesses[word].push($(ui.draggable).attr('data-letter'));
if ($(this).text() == $(ui.draggable).text().trim()) {
$(this).addClass('wordglow3');
} else {
$(this).addClass('wordglow');
}
if (guesses[word].length == 3) {
if (guesses[word].join('') == word) {
$('td[data-word=' + word + ']').addClass('wordglow2');
} else {
$('td[data-word=' + word + ']').addClass("wordglow4");
guesses[word].splice(0, guesses[word].length);
}
}
},
activate: function(event, ui) {
word = $(this).data('word');
// try to remove the class
$('td[data-word=' + word + ']').removeClass('wordglow').removeClass('wordglow4').removeClass('wordglow3');
}
});
}
HTML的可拖动对象是...。
<div class="squares">
<div id="drag1" class="drag ui-widget-content box-style2" tabindex="0" data-letter="a">
<p>a</p>
</div>
<div id="drag2" class="drag ui-widget-content box-style" tabindex="0" data-letter="b">
<p>b</p>
</div>
<div id="drag3" class="drag ui-widget-content box-style" tabindex="0" data-letter="c">
<p>c</p>
</div>
<div id="drag4" class="drag ui-widget-content box-style" tabindex="0" data-letter="d">
<p>d</p>
</div>
<div id="drag5" class="drag ui-widget-content box-style2" tabindex="0" data-letter="e">
<p>e</p>
</div>
<div id="drag6" class="drag ui-widget-content box-style" tabindex="0" data-letter="f">
<p>f</p>
</div>
<div id="drag7" class="drag ui-widget-content box-style" tabindex="0" data-letter="g">
<p>g</p>
</div>
<div id="drag8" class="drag ui-widget-content box-style" tabindex="0" data-letter="h">
<p>h</p>
</div>
<div id="drag9" class="drag ui-widget-content box-style2" tabindex="0" data-letter="i">
<p>i</p>
</div>
<div id="drag10" class="drag ui-widget-content box-style" tabindex="0" data-letter="j">
<p>j</p>
</div>
<div id="drag11" class="drag ui-widget-content box-style" tabindex="0" data-letter="k">
<p>k</p>
</div>
<div id="drag12" class="drag ui-widget-content box-style" tabindex="0" data-letter="l">
<p>l</p>
</div>
<div id="drag13" class="drag ui-widget-content box-style" tabindex="0" data-letter="m">
<p>m</p>
</div>
<div id="drag14" class="drag ui-widget-content box-style" tabindex="0" data-letter="n">
<p>n</p>
</div>
<div id="drag15" class="drag ui-widget-content box-style2" tabindex="0" data-letter="o">
<p>o</p>
</div>
<div id="drag16" class="drag ui-widget-content box-style" tabindex="0" data-letter="p">
<p>p</p>
</div>
<div id="drag17" class="drag ui-widget-content box-style" tabindex="0" data-letter="r">
<p>r</p>
</div>
<div id="drag18" class="drag ui-widget-content box-style" tabindex="0" data-letter="s">
<p>s</p>
</div>
<div id="drag19" class="drag ui-widget-content box-style" tabindex="0" data-letter="t">
<p>t</p>
</div>
<div id="drag20" class="drag ui-widget-content box-style2" tabindex="0" data-letter="u">
<p>u</p>
</div>
</div>
最佳答案
就个人而言,我不喜欢使用CSS来达到这种效果的想法。为什么不把字母实际上放在单词的第一个空白处呢?这似乎达到了预期的效果:
http://jsfiddle.net/nickaknudson/sYJwm/
$('.drop').droppable({
...
drop: function(event, ui) {
word = $(this).data('word');
//change context from this to that
that = $('.spellword')[guesses[word].length];
guesses[word].push($(ui.draggable).attr('data-letter'));
if ($(that).text() == $(ui.draggable).text().trim()) {
$(that).addClass('wordglow3');
} else {
$(that).addClass('wordglow');
}
if (guesses[word].length == 3) {
if (guesses[word].join('') == word) {
$('td[data-word=' + word + ']').addClass('wordglow2');
} else {
$('td[data-word=' + word + ']').addClass("wordglow4");
guesses[word].splice(0, guesses[word].length);
}
}
},
...
关于javascript - 放置时使可拖动的快照向左移动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11684347/