Vue Class绑定:字符串形式详解与应用
在Vue中,class绑定有多种形式,其中字符串形式是最基础且常用的一种。我将通过一个完整的示例展示其用法和优势。
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Vue Class绑定 - 字符串形式</title><script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script><style>* {margin: 0;padding: 0;box-sizing: border-box;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}body {background: linear-gradient(135deg, #1e3c72, #2a5298);min-height: 100vh;display: flex;justify-content: center;align-items: center;padding: 20px;color: #333;}.container {max-width: 900px;width: 100%;background: rgba(255, 255, 255, 0.95);border-radius: 16px;box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);overflow: hidden;}header {background: linear-gradient(90deg, #3498db, #2980b9);color: white;padding: 25px;text-align: center;}h1 {font-size: 2.5rem;margin-bottom: 10px;}.subtitle {font-size: 1.1rem;opacity: 0.9;}.content {padding: 30px;display: grid;grid-template-columns: 1fr 1fr;gap: 30px;}@media (max-width: 768px) {.content {grid-template-columns: 1fr;}}.demo-section {background: #f8f9fa;border-radius: 12px;padding: 25px;box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);}h2 {color: #2c3e50;margin-bottom: 20px;padding-bottom: 10px;border-bottom: 2px solid #3498db;}.controls {display: flex;flex-direction: column;gap: 15px;margin-bottom: 25px;}.control-group {display: flex;align-items: center;gap: 10px;}label {width: 120px;font-weight: 600;color: #2c3e50;}input[type="text"],select {flex: 1;padding: 10px;border: 2px solid #3498db;border-radius: 8px;font-size: 1rem;}button {padding: 12px;background: #3498db;color: white;border