评级系统组件
用于电子商务的 Retro/Vintage 评级系统组件,具有互补色方案。响应式,支持深色模式。不需要 JavaScript,只需要带有 Tailwind CSS 的 HTML。
HTML 代码
<div class="bg-gray-200 dark:bg-gray-800 p-8 min-h-screen flex items-center justify-center font-mono">
<div class="bg-white dark:bg-gray-900 shadow-lg rounded-lg p-6 max-w-md w-full border-4 border-purple-500 dark:border-yellow-400 transform rotate-3 hover:rotate-0 transition-transform duration-300">
<div class="text-center mb-6">
<h2 class="text-3xl font-bold text-purple-700 dark:text-yellow-300 mb-2">Rate Our Product!</h2>
<p class="text-gray-600 dark:text-gray-400">Share your thoughts on this awesome vintage item.</p>
</div>
<div class="flex justify-center items-center mb-6 space-x-2">
<label for="rating" class="sr-only">Rating</label>
<input type="range" id="rating" name="rating" min="1" max="5" value="4" class="slider w-full h-2 rounded-lg appearance-none cursor-pointer bg-gradient-to-r from-purple-400 to-red-400 dark:from-yellow-400 dark:to-green-400 thumb-retro" style="--slider-track-color: #d1d5db; --slider-thumb-color: #8b5cf6;">
<span class="text-2xl font-bold text-purple-700 dark:text-yellow-300" id="rating-value">4</span>
</div>
<div class="grid grid-cols-5 gap-2 mb-6">
<button class="rating-star group relative w-12 h-12 flex items-center justify-center bg-gray-300 dark:bg-gray-700 rounded-full text-2xl text-gray-500 dark:text-gray-400 hover:bg-purple-400 hover:text-white dark:hover:bg-yellow-400 dark:hover:text-gray-900 transition-all duration-200 shadow-md transform hover:scale-110">
<span class="star-icon">⭐</span>
<span class="tooltip absolute bottom-full left-1/2 -translate-x-1/2 mb-2 px-3 py-1 bg-gray-800 text-white text-xs rounded opacity-0 group-hover:opacity-100 transition-opacity duration-200 pointer-events-none">Terrible</span>
</button>
<button class="rating-star group relative w-12 h-12 flex items-center justify-center bg-gray-300 dark:bg-gray-700 rounded-full text-2xl text-gray-500 dark:text-gray-400 hover:bg-purple-400 hover:text-white dark:hover:bg-yellow-400 dark:hover:text-gray-900 transition-all duration-200 shadow-md transform hover:scale-110">
<span class="star-icon">⭐⭐</span>
<span class="tooltip absolute bottom-full left-1/2 -translate-x-1/2 mb-2 px-3 py-1 bg-gray-800 text-white text-xs rounded opacity-0 group-hover:opacity-100 transition-opacity duration-200 pointer-events-none">Bad</span>
</button>
<button class="rating-star group relative w-12 h-12 flex items-center justify-center bg-gray-300 dark:bg-gray-700 rounded-full text-2xl text-gray-500 dark:text-gray-400 hover:bg-purple-400 hover:text-white dark:hover:bg-yellow-400 dark:hover:text-gray-900 transition-all duration-200 shadow-md transform hover:scale-110">
<span class="star-icon">⭐⭐⭐</span>
<span class="tooltip absolute bottom-full left-1/2 -translate-x-1/2 mb-2 px-3 py-1 bg-gray-800 text-white text-xs rounded opacity-0 group-hover:opacity-100 transition-opacity duration-200 pointer-events-none">Okay</span>
</button>
<button class="rating-star group relative w-12 h-12 flex items-center justify-center bg-gray-300 dark:bg-gray-700 rounded-full text-2xl text-gray-500 dark:text-gray-400 hover:bg-purple-400 hover:text-white dark:hover:bg-yellow-400 dark:hover:text-gray-900 transition-all duration-200 shadow-md transform hover:scale-110">
<span class="star-icon">⭐⭐⭐⭐</span>
<span class="tooltip absolute bottom-full left-1/2 -translate-x-1/2 mb-2 px-3 py-1 bg-gray-800 text-white text-xs rounded opacity-0 group-hover:opacity-100 transition-opacity duration-200 pointer-events-none">Good</span>
</button>
<button class="rating-star group relative w-12 h-12 flex items-center justify-center bg-gray-300 dark:bg-gray-700 rounded-full text-2xl text-gray-500 dark:text-gray-400 hover:bg-purple-400 hover:text-white dark:hover:bg-yellow-400 dark:hover:text-gray-900 transition-all duration-200 shadow-md transform hover:scale-110">
<span class="star-icon">⭐⭐⭐⭐⭐</span>
<span class="tooltip absolute bottom-full left-1/2 -translate-x-1/2 mb-2 px-3 py-1 bg-gray-800 text-white text-xs rounded opacity-0 group-hover:opacity-100 transition-opacity duration-200 pointer-events-none">Excellent</span>
</button>
</div>
<div class="mb-6">
<label for="feedback" class="block text-gray-700 dark:text-gray-300 text-sm font-bold mb-2">Your Feedback:</label>
<textarea id="feedback" name="feedback" rows="4" class="shadow appearance-none border border-gray-400 dark:border-gray-600 rounded w-full py-3 px-4 text-gray-700 dark:text-gray-200 leading-tight focus:outline-none focus:ring-4 focus:ring-purple-300 dark:focus:ring-yellow-300 bg-gray-100 dark:bg-gray-700 resize-none placeholder-gray-500 dark:placeholder-gray-400 font-mono" placeholder="Tell us more about your experience..."></textarea>
</div>
<div class="flex flex-col sm:flex-row justify-between items-center space-y-4 sm:space-y-0 sm:space-x-4">
<button class="bg-purple-600 hover:bg-purple-700 text-white font-bold py-3 px-6 rounded-lg focus:outline-none focus:shadow-outline transition-all duration-200 transform hover:scale-105 shadow-lg border-2 border-purple-800 dark:bg-yellow-500 dark:hover:bg-yellow-600 dark:text-gray-900 dark:border-yellow-700 w-full sm:w-auto">
Submit Rating
</button>
<button class="bg-gray-400 hover:bg-gray-500 text-white font-bold py-3 px-6 rounded-lg focus:outline-none focus:shadow-outline transition-all duration-200 transform hover:scale-105 shadow-lg border-2 border-gray-600 dark:bg-gray-600 dark:hover:bg-gray-700 dark:border-gray-800 w-full sm:w-auto">
Cancel
</button>
</div>
</div>
</div>
<style>
/* Retro slider thumb styles */
.thumb-retro::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
background: #8b5cf6; /* Purple */
cursor: pointer;
border-radius: 50%;
border: 3px solid #f9fafb; /* White */
box-shadow: 0 0 0 4px rgba(139, 92, 246, 0.4); /* Purple glow */
transition: background 0.15s ease-in-out, border-color 0.15s ease-in-out;
}
.dark .thumb-retro::-webkit-slider-thumb {
background: #fbbf24; /* Yellow */
border-color: #111827; /* Dark gray */
box-shadow: 0 0 0 4px rgba(251, 191, 36, 0.4); /* Yellow glow */
}
.thumb-retro::-moz-range-thumb {
width: 25px;
height: 25px;
background: #8b5cf6; /* Purple */
cursor: pointer;
border-radius: 50%;
border: 3px solid #f9fafb; /* White */
box-shadow: 0 0 0 4px rgba(139, 92, 246, 0.4); /* Purple glow */
transition: background 0.15s ease-in-out, border-color 0.15s ease-in-out;
}
.dark .thumb-retro::-moz-range-thumb {
background: #fbbf24; /* Yellow */
border-color: #111827; /* Dark gray */
box-shadow: 0 0 0 4px rgba(251, 191, 36, 0.4); /* Yellow glow */
}
/* Tooltip for stars */
.group .tooltip {
display: none;
}
.group:hover .tooltip {
display: block;
}
/* Custom styles for star rating selection (active state - optional, can be done with JS) */
.rating-star:hover .star-icon {
animation: pulse-star 0.3s ease-in-out infinite alternate;
}
@keyframes pulse-star {
from { transform: scale(1); }
to { transform: scale(1.2); }
}
/* Hide the default range input track */
input[type="range"]::-webkit-slider-runnable-track {
background: var(--slider-track-color);
border-radius: 9999px;
}
input[type="range"]::-moz-range-track {
background: var(--slider-track-color);
border-radius: 9999px;
}
</style>