回答編集履歴
1
別パターンのコードを追記
answer
CHANGED
@@ -3,10 +3,189 @@
|
|
3
3
|
|
4
4
|
[サンプルコード](https://codesandbox.io/s/vue-template-q512z?fontsize=14)
|
5
5
|
|
6
|
+
一応2パターン載せておきます。①のほうがロジックはシンプルになりますが、管理する状態が増えます。
|
7
|
+
②は管理する状態は減りますが、ロジックは若干複雑になります。
|
8
|
+
|
9
|
+
今回の場合は①のほうが読みやすいかもしれません。
|
10
|
+
|
11
|
+
①開閉状態を別の連想配列として管理するパターン
|
12
|
+
|
6
13
|
```js
|
7
14
|
<template>
|
8
15
|
<div>
|
16
|
+
<div
|
17
|
+
v-for="skill in skills"
|
18
|
+
class="accordion"
|
19
|
+
v-bind:key="skill.id"
|
20
|
+
v-bind:style="{ border: '8px' + ' ' + 'solid' + ' ' + skill.bgColor, backgroundColor: skill.bgColor }"
|
21
|
+
>
|
9
22
|
<div
|
23
|
+
class="label"
|
24
|
+
v-on:click="toggleAccordion(skill.id)"
|
25
|
+
v-bind:style="{ backgroundColor: skill.bgColor }"
|
26
|
+
>
|
27
|
+
<h2>
|
28
|
+
{{ skill.name }}
|
29
|
+
<i
|
30
|
+
class="fa fa-angle-down label-icon"
|
31
|
+
v-bind:class="{ rotate : show[skill.id] }"
|
32
|
+
></i>
|
33
|
+
</h2>
|
34
|
+
</div>
|
35
|
+
<transition name="slide">
|
36
|
+
<div class="box" v-show="show[skill.id]">
|
37
|
+
<!-- <img v-bind:src="require(`../assets/images/${skill.name}.png`)"> -->
|
38
|
+
<p>{{ skill.description }}</p>
|
39
|
+
</div>
|
40
|
+
</transition>
|
41
|
+
</div>
|
42
|
+
</div>
|
43
|
+
</template>
|
44
|
+
|
45
|
+
<script>
|
46
|
+
export default {
|
47
|
+
name: "HelloWorld",
|
48
|
+
data() {
|
49
|
+
return {
|
50
|
+
skills: [
|
51
|
+
{
|
52
|
+
id: 1,
|
53
|
+
name: "HTML・CSS",
|
54
|
+
bgColor: "red",
|
55
|
+
description: `
|
56
|
+
text
|
57
|
+
`
|
58
|
+
},
|
59
|
+
{
|
60
|
+
id: 2,
|
61
|
+
name: "JavaScript",
|
62
|
+
bgColor: "gold",
|
63
|
+
description: `
|
64
|
+
text
|
65
|
+
`
|
66
|
+
},
|
67
|
+
{
|
68
|
+
id: 3,
|
69
|
+
name: "Vue.js",
|
70
|
+
bgColor: "forestgreen",
|
71
|
+
description: `
|
72
|
+
text
|
73
|
+
`
|
74
|
+
},
|
75
|
+
{
|
76
|
+
id: 4,
|
77
|
+
name: "Sass",
|
78
|
+
bgColor: "hotpink",
|
79
|
+
description: `
|
80
|
+
text
|
81
|
+
`
|
82
|
+
},
|
83
|
+
{
|
84
|
+
id: 5,
|
85
|
+
name: "Firebase",
|
86
|
+
bgColor: "orange",
|
87
|
+
description: `
|
88
|
+
text
|
89
|
+
`
|
90
|
+
},
|
91
|
+
{
|
92
|
+
id: 6,
|
93
|
+
name: "Git・Github",
|
94
|
+
bgColor: "black",
|
95
|
+
description: `
|
96
|
+
text
|
97
|
+
`
|
98
|
+
},
|
99
|
+
{
|
100
|
+
id: 7,
|
101
|
+
name: "webpack",
|
102
|
+
bgColor: "skyblue",
|
103
|
+
description: `
|
104
|
+
text
|
105
|
+
`
|
106
|
+
}
|
107
|
+
],
|
108
|
+
show: {
|
109
|
+
1: false,
|
110
|
+
2: false,
|
111
|
+
3: false,
|
112
|
+
4: false,
|
113
|
+
5: false,
|
114
|
+
6: false,
|
115
|
+
7: false
|
116
|
+
}
|
117
|
+
};
|
118
|
+
},
|
119
|
+
methods: {
|
120
|
+
toggleAccordion(skillId) {
|
121
|
+
this.show[skillId] = !this.show[skillId];
|
122
|
+
}
|
123
|
+
}
|
124
|
+
};
|
125
|
+
</script>
|
126
|
+
|
127
|
+
<!-- Add "scoped" attribute to limit CSS to this component only -->
|
128
|
+
<style lang="scss" scoped>
|
129
|
+
.accordion {
|
130
|
+
width: 400px;
|
131
|
+
.label {
|
132
|
+
height: 30px;
|
133
|
+
margin-bottom: 10px;
|
134
|
+
text-align: center;
|
135
|
+
color: white;
|
136
|
+
cursor: pointer;
|
137
|
+
h2 {
|
138
|
+
font-weight: bold;
|
139
|
+
line-height: 30px;
|
140
|
+
}
|
141
|
+
.label-icon {
|
142
|
+
margin-left: 10px;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
.box {
|
146
|
+
overflow: hidden;
|
147
|
+
background-color: white;
|
148
|
+
width: 100%;
|
149
|
+
display: flex;
|
150
|
+
img {
|
151
|
+
display: block;
|
152
|
+
width: 100px;
|
153
|
+
height: 100px;
|
154
|
+
}
|
155
|
+
}
|
156
|
+
}
|
157
|
+
.rotate {
|
158
|
+
transform: rotate(180deg);
|
159
|
+
transition: 0.3s;
|
160
|
+
}
|
161
|
+
.slide-enter-active {
|
162
|
+
transition-duration: 0.3s;
|
163
|
+
transition-timing-function: ease-in;
|
164
|
+
}
|
165
|
+
.slide-leave-active {
|
166
|
+
transition-duration: 0.3s;
|
167
|
+
transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
|
168
|
+
}
|
169
|
+
.slide-enter-to,
|
170
|
+
.slide-leave {
|
171
|
+
max-height: 100px;
|
172
|
+
overflow: hidden;
|
173
|
+
}
|
174
|
+
.slide-enter,
|
175
|
+
.slide-leave-to {
|
176
|
+
max-height: 0;
|
177
|
+
overflow: hidden;
|
178
|
+
}
|
179
|
+
</style>
|
180
|
+
|
181
|
+
```
|
182
|
+
|
183
|
+
②開閉状態を既存のskills配列の中で管理するパターン
|
184
|
+
|
185
|
+
```js
|
186
|
+
<template>
|
187
|
+
<div>
|
188
|
+
<div
|
10
189
|
v-for="skill in skills"
|
11
190
|
class="accordion"
|
12
191
|
v-bind:key="skill.id"
|