CSS3星星评级特效

CSS3与JS实现星星评级特效

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS3星星评级特效</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #f5f5f5;
            font-family: 'Arial', sans-serif;
            flex-direction: column;
        }

        .rating-container {
            text-align: center;
            margin-bottom: 50px;
        }

        h1 {
            color: #333;
            margin-bottom: 30px;
            font-size: 2.5em;
        }

        .rating {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: row-reverse;
            margin-bottom: 20px;
        }

        .rating input {
            display: none;
        }

        .rating label {
            cursor: pointer;
            width: 50px;
            height: 50px;
            margin: 0 5px;
            background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23ddd"><path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"/></svg>');
            background-size: cover;
            transition: all 0.3s;
        }

        .rating input:checked ~ label,
        .rating input:hover ~ label {
            background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23FFD700"><path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"/></svg>');
        }

        .rating label:hover,
        .rating label:hover ~ label {
            background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23FFD700"><path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"/></svg>');
        }

        .result {
            font-size: 1.5em;
            color: #FFA500;
            margin-top: 20px;
            height: 30px;
        }

        .animated-stars {
            position: relative;
            width: 250px;
            height: 50px;
            margin: 30px auto;
        }

        .star {
            position: absolute;
            width: 50px;
            height: 50px;
            background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23FFD700"><path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"/></svg>');
            background-size: cover;
            opacity: 0;
            animation: starPulse 2s infinite ease-in-out;
        }

        .star:nth-child(1) {
            left: 0;
            animation-delay: 0s;
        }

        .star:nth-child(2) {
            left: 50px;
            animation-delay: 0.2s;
        }

        .star:nth-child(3) {
            left: 100px;
            animation-delay: 0.4s;
        }

        .star:nth-child(4) {
            left: 150px;
            animation-delay: 0.6s;
        }

        .star:nth-child(5) {
            left: 200px;
            animation-delay: 0.8s;
        }

        @keyframes starPulse {
            0%, 100% {
                transform: scale(1);
                opacity: 0;
            }
            50% {
                transform: scale(1.2);
                opacity: 1;
            }
        }

        .link {
            margin-top: 50px;
        }

        .link a {
            color: #333;
            background: rgba(255, 215, 0, 0.3);
            padding: 10px 20px;
            border-radius: 30px;
            text-decoration: none;
            font-weight: bold;
            transition: all 0.3s;
        }

        .link a:hover {
            background: rgba(255, 215, 0, 0.5);
            box-shadow: 0 0 15px rgba(255, 215, 0, 0.3);
        }
    </style>
</head>
<body>
    <div class="rating-container">
        <h1>请为我们的服务评分</h1>
        
        <form class="rating">
            <input type="radio" id="star5" name="rating" value="5">
            <label for="star5"></label>
            
            <input type="radio" id="star4" name="rating" value="4">
            <label for="star4"></label>
            
            <input type="radio" id="star3" name="rating" value="3">
            <label for="star3"></label>
            
            <input type="radio" id="star2" name="rating" value="2">
            <label for="star2"></label>
            
            <input type="radio" id="star1" name="rating" value="1">
            <label for="star1"></label>
        </form>
        
        <div class="result" id="result">您尚未评分</div>
        
        <div class="animated-stars">
            <div class="star"></div>
            <div class="star"></div>
            <div class="star"></div>
            <div class="star"></div>
            <div class="star"></div>
        </div>
    </div>
    
    <div class="link">
       
    </div>

    <script>
        const ratingInputs = document.querySelectorAll('.rating input');
        const resultDiv = document.getElementById('result');
        
        ratingInputs.forEach(input => {
            input.addEventListener('change', (e) => {
                const rating = e.target.value;
                let message = '';
                
                switch(rating) {
                    case '5':
                        message = '非常满意,感谢您的五星好评!';
                        break;
                    case '4':
                        message = '感谢您的四星评价,我们会继续努力!';
                        break;
                    case '3':
                        message = '感谢您的三星评价,我们会改进服务!';
                        break;
                    case '2':
                        message = '感谢您的反馈,我们会认真改进!';
                        break;
                    case '1':
                        message = '非常抱歉,我们会立即改进服务!';
                        break;
                    default:
                        message = '您尚未评分';
                }
                
                resultDiv.textContent = message;
                resultDiv.style.color = rating >= 4 ? '#4CAF50' : rating >= 3 ? '#FFA500' : '#F44336';
            });
        });
    </script>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值