我在网上建立了一些不错的旋转轨迹,并且一切正常,但是唯一的问题是如何获取选定的卡数据ID? 我尝试了很多解决方案,但搜索失败了,因为旋转后我无法定义所选的卡,因为我想搜索数据库并获取该卡的信息。
var $cardList = $('.cardList').first(),
$cards = $('.card'),
width = 100,
randomize = true,
distance = 20 * width;
for (var i = 0; i < 50; i++) {
$cards.clone().appendTo($cardList);
}
function spin() {
var newMargin = 0,
newDistance = distance,
speed = 5000;
if (randomize) {
newDistance = Math.floor(Math.random() * $cards.length * 5);
newDistance += $cards.length * 5;
newDistance *= width;
}
newMargin = -(newDistance);
$cards.first().animate({
marginLeft: newMargin
}, speed);
}
$('#spin').click(function() {
$cards.first().css('margin-left', 0);
spin();
return false;
});
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
.cardList {
height: 100px;
width: 302px;
position: relative;
margin: 10px;
border: 1px solid #33e;
overflow: hidden;
white-space: nowrap;
}
.card {
display: inline-block;
text-align: center;
height: 100px;
width: 100px;
line-height: 100px;
background-color: #99e;
font-family: monospace;
font-size: 2em;
color: #444;
border-left: 1px solid #33e;
border-right: 1px solid #33e;
}
.cardList::before,
.cardList::after {
content: '';
display: block;
z-index: 100;
width: 0px;
height: 0px;
transform: translateX(-50%);
border-left: 8px solid transparent;
border-right: 8px solid transparent;
}
.cardList::before {
position: absolute;
top: 0px;
left: 50%;
border-top: 12px solid #33e;
}
.cardList::after {
position: absolute;
bottom: 0px;
left: 50%;
border-bottom: 12px solid #33e;
}
div.controls,
button {
margin: 10px;
padding: 8px;
font-family: monospace;
}
div.controls input {
width: 48px;
padding: 2px;
text-align: center;
font-family: monospace;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cardList">
<div class="card" data-id="1">1</div>
<div class="card" data-id="2">2</div>
<div class="card" data-id="3">3</div>
<div class="card" data-id="4">4</div>
<div class="card" data-id="5">5</div>
<div class="card" data-id="6">6</div>
<div class="card" data-id="7">7</div>
<div class="card" data-id="8">8</div>
</div>
<button id="spin">Spin</button>
答案 0 :(得分:1)
您甚至可以在微调器启动之前就知道答案。到目前为止,似乎没有任何失败的猜测。
var numCardsPassed = newDistance / 100;
在这里,我将生成的距离除以一张纸牌的宽度,得出多少张纸牌将被随机播放。
var answer = Math.floor(STARTING_INDEX + (numCardsPassed % $cards.length));
在这里,我计算了经过八次卡旋转numCardsPassed
(从索引/卡2开始)后,它将落在哪张卡上。
这种方法的妙处在于,您可以在卡片改组时从数据库中获取数据,这意味着您可以在动画完成后立即显示数据。
// the card the arrows start on, in this case, card 2
const STARTING_INDEX = 2;
var $cardList = $('.cardList').first(),
$cards = $('.card'),
width = 100,
randomize = true,
distance = 20 * width;
for (var i = 0; i < 50; i++) {
$cards.clone().appendTo($cardList);
}
function spin() {
var newMargin = 0,
newDistance = distance,
speed = 5000;
if (randomize) {
newDistance = Math.floor(Math.random() * $cards.length * 5);
newDistance += $cards.length * 5;
newDistance *= width;
}
var numCardsPassed = newDistance / 100;
var answer = Math.floor(STARTING_INDEX + (numCardsPassed % $cards.length));
console.log("Answer: " + answer);
newMargin = -(newDistance);
$cards.first().animate({
marginLeft: newMargin
}, speed, function() {
console.log("animation is done");
});
}
$('#spin').click(function() {
$cards.first().css('margin-left', 0);
spin();
return false;
});
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
.cardList {
height: 100px;
width: 302px;
position: relative;
margin: 10px;
border: 1px solid #33e;
overflow: hidden;
white-space: nowrap;
}
.card {
display: inline-block;
text-align: center;
height: 100px;
width: 100px;
line-height: 100px;
background-color: #99e;
font-family: monospace;
font-size: 2em;
color: #444;
border-left: 1px solid #33e;
border-right: 1px solid #33e;
}
.cardList::before,
.cardList::after {
content: '';
display: block;
z-index: 100;
width: 0px;
height: 0px;
transform: translateX(-50%);
border-left: 8px solid transparent;
border-right: 8px solid transparent;
}
.cardList::before {
position: absolute;
top: 0px;
left: 50%;
border-top: 12px solid #33e;
}
.cardList::after {
position: absolute;
bottom: 0px;
left: 50%;
border-bottom: 12px solid #33e;
}
div.controls,
button {
margin: 10px;
padding: 8px;
font-family: monospace;
}
div.controls input {
width: 48px;
padding: 2px;
text-align: center;
font-family: monospace;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cardList">
<div class="card" data-id="1">1</div>
<div class="card" data-id="2">2</div>
<div class="card" data-id="3">3</div>
<div class="card" data-id="4">4</div>
<div class="card" data-id="5">5</div>
<div class="card" data-id="6">6</div>
<div class="card" data-id="7">7</div>
<div class="card" data-id="8">8</div>
</div>
<button id="spin">Spin</button>