以下是一个使用JS+CSS+HTML设计的战斗界面动画的示例代码:

<!DOCTYPE html>
<html>
<head>
<style>
    body {
        background-color: green;
    }
    
    #container {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
    }
    
    .box {
        width: 200px;
        height: 200px;
        background-color: white;
        border: 2px solid black;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 20px;
        font-weight: bold;
        transition: background-color 0.5s;
    }
    
    .box.shake {
        animation: shake 0.5s;
    }
    
    @keyframes shake {
        0% { transform: translateX(0); }
        25% { transform: translateX(-10px); }
        50% { transform: translateX(10px); }
        75% { transform: translateX(-10px); }
        100% { transform: translateX(0); }
    }
    
    .box.low-health {
        background-color: red;
    }
</style>
</head>
<body>
    <div id="container">
        <div id="boxA" class="box">100</div>
        <div id="boxB" class="box">100</div>
    </div>
    
    <script>
        var param = '{"win":"A","first":"A","process":{"A":"1,2,3,4,5,6,7,8,10,2,3,3,5,1,2,3,5,7,4","B":"1,2,4,3,5,5,5,5,4,6,2,8,3,7,10,8,2,10,10"}}';
        var data = JSON.parse(param);
        var boxA = document.getElementById('boxA');
        var boxB = document.getElementById('boxB');
        
        var processA = data.process.A.split(',');
        var processB = data.process.B.split(',');
        
        var currentA = 100;
        var currentB = 100;
        
        function startBattle() {
            if (data.first === 'A') {
                attackA();
            } else {
                attackB();
            }
        }
        
        function attackA() {
            if (currentB <= 0) {
                boxA.innerText = 'A';
                boxB.innerText = '';
                boxB.classList.remove('shake');
                return;
            }
            
            var damage = parseInt(processA.shift());
            currentB -= damage;
            
            boxA.innerText = currentA;
            boxB.innerText = currentB;
            
            if (currentB <= 0) {
                boxA.innerText = 'A';
                boxB.innerText = '';
                boxB.classList.remove('shake');
                return;
            }
            
            if (currentB <= 20) {
                boxB.classList.add('low-health');
            }
            
            boxB.classList.add('shake');
            
            setTimeout(attackB, 1000);
        }
        
        function attackB() {
            if (currentA <= 0) {
                boxA.innerText = '';
                boxB.innerText = 'B';
                boxA.classList.remove('shake');
                return;
            }
            
            var damage = parseInt(processB.shift());
            currentA -= damage;
            
            boxA.innerText = currentA;
            boxB.innerText = currentB;
            
            if (currentA <= 0) {
                boxA.innerText = '';
                boxB.innerText = 'B';
                boxA.classList.remove('shake');
                return;
            }
            
            if (currentA <= 20) {
                boxA.classList.add('low-health');
            }
            
            boxA.classList.add('shake');
            
            setTimeout(attackA, 1000);
        }
        
        startBattle();
    </script>
</body>
</html>

在上面的代码中,使用了一个包含两个方框的容器,通过CSS样式设置了方框的样式和动画效果。在JS代码中,根据参数设置初始血量和战斗过程,并使用setTimeout函数来模拟战斗过程的延迟。当方框A攻击方框B时,方框B会抖动并减少血量;当方框B攻击方框A时,方框A会抖动并减少血量。当血量低于20时,背景颜色会变成红色。最终根据获胜方的参数,显示获胜方的标识


原文地址: https://www.cveoy.top/t/topic/iSi4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录