gpt4 book ai didi

javascript - Jquery 旋转轨道与项目

转载 作者:行者123 更新时间:2023-12-03 00:24:27 25 4
gpt4 key购买 nike

我在网络上建立了一些不错的旋转轨道,一切都很完美,但唯一的问题是如何获取选定的卡数据 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>

最佳答案

在旋转器启动之前您就可以知道答案。到目前为止,这似乎有效,没有任何失败的猜测。

var numCardsPassed = newDistance/100;在这里,我将生成的距离除以卡片的宽度,以获得将洗牌的卡片数量。

var answer = Math.floor(STARTING_INDEX + (numCardsPassed % $cards.length));在这里,我计算出在经过 8 个卡片轮换 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>

关于javascript - Jquery 旋转轨道与项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54152238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com