1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>SVG 身体部位选择器</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; } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); min-height: 100vh; display: flex; flex-direction: column; align-items: center; padding: 20px; color: #2c3e50; } .container { max-width: 1200px; width: 100%; display: flex; flex-direction: column; align-items: center; } header { text-align: center; margin-bottom: 30px; width: 100%; } h1 { font-size: 2.5rem; color: #3498db; margin-bottom: 10px; text-shadow: 1px 1px 3px rgba(0,0,0,0.1); } .description { font-size: 1.1rem; color: #7f8c8d; max-width: 800px; margin: 0 auto; } .content { display: flex; flex-wrap: wrap; justify-content: center; gap: 30px; width: 100%; } .svg-container { background: white; border-radius: 15px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1); padding: 20px; flex: 1; min-width: 500px; max-width: 700px; overflow: auto; } .controls { background: white; border-radius: 15px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1); padding: 20px; flex: 1; min-width: 300px; max-width: 400px; } .controls h2 { color: #3498db; margin-bottom: 20px; text-align: center; } .body-part-list { list-style: none; } .body-part-item { display: flex; align-items: center; padding: 12px 15px; margin-bottom: 10px; background: #f8f9fa; border-radius: 8px; cursor: pointer; transition: all 0.3s ease; } .body-part-item:hover { background: #e8f4fc; transform: translateY(-2px); } .body-part-item.selected { background: #d1edff; border-left: 4px solid #3498db; } .color-indicator { width: 20px; height: 20px; border-radius: 50%; margin-right: 15px; background: rgba(53, 254, 255, 0.4); } .instructions { margin-top: 30px; background: white; border-radius: 15px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1); padding: 20px; width: 100%; max-width: 800px; } .instructions h2 { color: #3498db; margin-bottom: 15px; text-align: center; } .instructions ul { padding-left: 20px; } .instructions li { margin-bottom: 10px; line-height: 1.5; } .highlight { background: #fff8e1; padding: 2px 5px; border-radius: 4px; font-family: monospace; } @media (max-width: 900px) { .content { flex-direction: column; align-items: center; } .svg-container, .controls { min-width: 100%; } } </style> </head> <body> <div id="app" class="container"> <header> <h1>身体部位选择器</h1> <p class="description">点击下方身体部位或右侧列表项可查看SVG层级关系。文本始终显示在路径上方。</p> </header> <div class="content"> <div class="svg-container"> <svg width="100%" height="500" viewBox="0 0 1000 600"> <!-- 使用g元素循环渲染身体部位 --> <g v-for="item in bodyParts" :key="item.uuid"> <!-- 先绘制路径 --> <path :d="item.position" :fill="item.checked ? 'rgba(53, 254, 255, 0.4)' : 'transparent'" stroke="#3498db" stroke-width="2" @click="toggleSelection(item)" class="body-path" /> <!-- 后绘制文本(文本将显示在路径上方) --> <text :x="item.x" :y="item.y" text-anchor="middle" dominant-baseline="middle" font-size="20" :fill="item.checked ? '#2c3e50' : '#7f8c8d'" font-weight="bold" @click="toggleSelection(item)" class="body-text" > {{ item.name }} </text> </g> </svg> </div> <div class="controls"> <h2>身体部位列表</h2> <ul class="body-part-list"> <li v-for="item in bodyParts" :key="item.uuid" @click="toggleSelection(item)" :class="['body-part-item', {selected: item.checked}]" > <div class="color-indicator" :style="{opacity: item.checked ? 1 : 0.2}"></div> <span>{{ item.name }}</span> </li> </ul> </div> </div> <div class="instructions"> <h2>实现原理说明</h2> <ul> <li>在SVG中,元素的绘制顺序决定了它们的层级关系 - 后绘制的元素会显示在先绘制的元素之上。</li> <li>要让文本显示在路径上层,只需确保<strong class="highlight">文本元素在路径元素之后定义</strong>。</li> <li>在此示例中,我们使用Vue的v-for指令循环渲染身体部位,每个部位组内先绘制路径再绘制文本。</li> <li>点击身体部位或列表项可以切换选中状态,选中的部位会以半透明青色显示。</li> </ul> </div> </div>
<script> new Vue({ el: '#app', data: { bodyParts: [ { name: '头部', uuid: '1', x: 500, y: 100, position: 'M450,100 C450,50 550,50 550,100 C550,150 450,150 450,100 Z', checked: false }, { name: '躯干', uuid: '2', x: 500, y: 280, position: 'M450,180 L450,380 550,380 550,180 Z', checked: false }, { name: '左臂', uuid: '3', x: 350, y: 280, position: 'M450,200 L350,200 300,380 400,380 Z', checked: false }, { name: '右臂', uuid: '4', x: 650, y: 280, position: 'M550,200 L650,200 700,380 600,380 Z', checked: false }, { name: '左腿', uuid: '5', x: 430, y: 480, position: 'M450,380 L430,380 420,580 450,580 Z', checked: false }, { name: '右腿', uuid: '6', x: 570, y: 480, position: 'M550,380 L570,380 580,580 550,580 Z', checked: false } ] }, methods: { toggleSelection(item) { item.checked = !item.checked; } } }); </script> </body> </html>
|