在 jQuery 中,為 Slot Machine 添加動(dòng)畫可以通過使用 animate()
函數(shù)實(shí)現(xiàn)。以下是一個(gè)簡單的示例,展示了如何為 Slot Machine 的旋轉(zhuǎn)輪盤添加動(dòng)畫效果:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div class="slot-machine">
<div class="wheel">
<div class="section"></div>
<div class="section"></div>
<div class="section"></div>
</div>
</div>
$(document).ready(function() {
// 獲取 Slot Machine 的旋轉(zhuǎn)輪盤
var $wheel = $('.wheel');
// 定義動(dòng)畫參數(shù)
var animationDuration = 1000; // 動(dòng)畫持續(xù)時(shí)間,單位為毫秒
var animationDelay = 200; // 動(dòng)畫延遲時(shí)間,單位為毫秒
var animationCount = 3; // 動(dòng)畫循環(huán)次數(shù)
var animationDirection = 'clockwise'; // 動(dòng)畫方向,順時(shí)針或逆時(shí)針
// 定義旋轉(zhuǎn)角度
var rotationAngle = 360 / animationCount;
// 定義動(dòng)畫函數(shù)
function rotateWheel() {
if (animationDirection === 'clockwise') {
$wheel.animate({
rotation: '+=' + rotationAngle + 'deg'
}, animationDuration, function() {
// 動(dòng)畫完成后,執(zhí)行回調(diào)函數(shù)
if (animationCount > 1) {
animationCount--;
rotateWheel();
}
});
} else {
$wheel.animate({
rotation: '-=' + rotationAngle + 'deg'
}, animationDuration, function() {
// 動(dòng)畫完成后,執(zhí)行回調(diào)函數(shù)
if (animationCount > 1) {
animationCount--;
rotateWheel();
}
});
}
}
// 啟動(dòng)動(dòng)畫
rotateWheel();
});
這個(gè)示例中,我們定義了一個(gè)名為 rotateWheel
的函數(shù),用于控制旋轉(zhuǎn)輪盤的動(dòng)畫效果。通過修改 animationDuration
、animationDelay
、animationCount
和 animationDirection
變量,你可以自定義動(dòng)畫的持續(xù)時(shí)間、延遲時(shí)間、循環(huán)次數(shù)和方向。