回答編集履歴

1

別パターンのコードを追記

2019/07/04 04:11

投稿

KuwabataK
KuwabataK

スコア306

test CHANGED
@@ -8,14 +8,372 @@
8
8
 
9
9
 
10
10
 
11
+ 一応2パターン載せておきます。①のほうがロジックはシンプルになりますが、管理する状態が増えます。
12
+
13
+ ②は管理する状態は減りますが、ロジックは若干複雑になります。
14
+
15
+
16
+
17
+ 今回の場合は①のほうが読みやすいかもしれません。
18
+
19
+
20
+
21
+ ①開閉状態を別の連想配列として管理するパターン
22
+
23
+
24
+
11
25
  ```js
12
26
 
13
27
  <template>
14
28
 
15
29
  <div>
16
30
 
31
+ <div
32
+
33
+ v-for="skill in skills"
34
+
35
+ class="accordion"
36
+
37
+ v-bind:key="skill.id"
38
+
39
+ v-bind:style="{ border: '8px' + ' ' + 'solid' + ' ' + skill.bgColor, backgroundColor: skill.bgColor }"
40
+
41
+ >
42
+
17
43
  <div
18
44
 
45
+ class="label"
46
+
47
+ v-on:click="toggleAccordion(skill.id)"
48
+
49
+ v-bind:style="{ backgroundColor: skill.bgColor }"
50
+
51
+ >
52
+
53
+ <h2>
54
+
55
+ {{ skill.name }}
56
+
57
+ <i
58
+
59
+ class="fa fa-angle-down label-icon"
60
+
61
+ v-bind:class="{ rotate : show[skill.id] }"
62
+
63
+ ></i>
64
+
65
+ </h2>
66
+
67
+ </div>
68
+
69
+ <transition name="slide">
70
+
71
+ <div class="box" v-show="show[skill.id]">
72
+
73
+ <!-- <img v-bind:src="require(`../assets/images/${skill.name}.png`)"> -->
74
+
75
+ <p>{{ skill.description }}</p>
76
+
77
+ </div>
78
+
79
+ </transition>
80
+
81
+ </div>
82
+
83
+ </div>
84
+
85
+ </template>
86
+
87
+
88
+
89
+ <script>
90
+
91
+ export default {
92
+
93
+ name: "HelloWorld",
94
+
95
+ data() {
96
+
97
+ return {
98
+
99
+ skills: [
100
+
101
+ {
102
+
103
+ id: 1,
104
+
105
+ name: "HTML・CSS",
106
+
107
+ bgColor: "red",
108
+
109
+ description: `
110
+
111
+ text
112
+
113
+ `
114
+
115
+ },
116
+
117
+ {
118
+
119
+ id: 2,
120
+
121
+ name: "JavaScript",
122
+
123
+ bgColor: "gold",
124
+
125
+ description: `
126
+
127
+ text
128
+
129
+ `
130
+
131
+ },
132
+
133
+ {
134
+
135
+ id: 3,
136
+
137
+ name: "Vue.js",
138
+
139
+ bgColor: "forestgreen",
140
+
141
+ description: `
142
+
143
+ text
144
+
145
+ `
146
+
147
+ },
148
+
149
+ {
150
+
151
+ id: 4,
152
+
153
+ name: "Sass",
154
+
155
+ bgColor: "hotpink",
156
+
157
+ description: `
158
+
159
+ text
160
+
161
+ `
162
+
163
+ },
164
+
165
+ {
166
+
167
+ id: 5,
168
+
169
+ name: "Firebase",
170
+
171
+ bgColor: "orange",
172
+
173
+ description: `
174
+
175
+ text
176
+
177
+ `
178
+
179
+ },
180
+
181
+ {
182
+
183
+ id: 6,
184
+
185
+ name: "Git・Github",
186
+
187
+ bgColor: "black",
188
+
189
+ description: `
190
+
191
+ text
192
+
193
+ `
194
+
195
+ },
196
+
197
+ {
198
+
199
+ id: 7,
200
+
201
+ name: "webpack",
202
+
203
+ bgColor: "skyblue",
204
+
205
+ description: `
206
+
207
+ text
208
+
209
+ `
210
+
211
+ }
212
+
213
+ ],
214
+
215
+ show: {
216
+
217
+ 1: false,
218
+
219
+ 2: false,
220
+
221
+ 3: false,
222
+
223
+ 4: false,
224
+
225
+ 5: false,
226
+
227
+ 6: false,
228
+
229
+ 7: false
230
+
231
+ }
232
+
233
+ };
234
+
235
+ },
236
+
237
+ methods: {
238
+
239
+ toggleAccordion(skillId) {
240
+
241
+ this.show[skillId] = !this.show[skillId];
242
+
243
+ }
244
+
245
+ }
246
+
247
+ };
248
+
249
+ </script>
250
+
251
+
252
+
253
+ <!-- Add "scoped" attribute to limit CSS to this component only -->
254
+
255
+ <style lang="scss" scoped>
256
+
257
+ .accordion {
258
+
259
+ width: 400px;
260
+
261
+ .label {
262
+
263
+ height: 30px;
264
+
265
+ margin-bottom: 10px;
266
+
267
+ text-align: center;
268
+
269
+ color: white;
270
+
271
+ cursor: pointer;
272
+
273
+ h2 {
274
+
275
+ font-weight: bold;
276
+
277
+ line-height: 30px;
278
+
279
+ }
280
+
281
+ .label-icon {
282
+
283
+ margin-left: 10px;
284
+
285
+ }
286
+
287
+ }
288
+
289
+ .box {
290
+
291
+ overflow: hidden;
292
+
293
+ background-color: white;
294
+
295
+ width: 100%;
296
+
297
+ display: flex;
298
+
299
+ img {
300
+
301
+ display: block;
302
+
303
+ width: 100px;
304
+
305
+ height: 100px;
306
+
307
+ }
308
+
309
+ }
310
+
311
+ }
312
+
313
+ .rotate {
314
+
315
+ transform: rotate(180deg);
316
+
317
+ transition: 0.3s;
318
+
319
+ }
320
+
321
+ .slide-enter-active {
322
+
323
+ transition-duration: 0.3s;
324
+
325
+ transition-timing-function: ease-in;
326
+
327
+ }
328
+
329
+ .slide-leave-active {
330
+
331
+ transition-duration: 0.3s;
332
+
333
+ transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
334
+
335
+ }
336
+
337
+ .slide-enter-to,
338
+
339
+ .slide-leave {
340
+
341
+ max-height: 100px;
342
+
343
+ overflow: hidden;
344
+
345
+ }
346
+
347
+ .slide-enter,
348
+
349
+ .slide-leave-to {
350
+
351
+ max-height: 0;
352
+
353
+ overflow: hidden;
354
+
355
+ }
356
+
357
+ </style>
358
+
359
+
360
+
361
+ ```
362
+
363
+
364
+
365
+ ②開閉状態を既存のskills配列の中で管理するパターン
366
+
367
+
368
+
369
+ ```js
370
+
371
+ <template>
372
+
373
+ <div>
374
+
375
+ <div
376
+
19
377
  v-for="skill in skills"
20
378
 
21
379
  class="accordion"