gpt4 book ai didi

javascript - 仅切换 Accordion 点击 vue.js

转载 作者:行者123 更新时间:2023-12-03 06:43:56 25 4
gpt4 key购买 nike

我正在构建一个简单的 vue Accordion ,当我单击一个 Accordion 时,它们都会打开,这不是我想要的行为。有没有办法只打开点击的那个?我以为“this”会解决这个问题,但我运气不好。

html:

<div id="accordion" class="accordion-container">
<div class="accordion" :class="accordionClasses">
<div class="accordion-header" @click="toggleAccordion">
Accordion 1
<i class="fal fa-plus" />
<!-- <i class="fal fa-minus" /> -->
</div>
<div class="accordion-body">
<div class="accordion-content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. <strong>Pellentesque risus mi</strong></div>
</div>
</div>
<div class="accordion" :class="accordionClasses">
<div class="accordion-header" @click="toggleAccordion">
Accordion 2
<i class="fal fa-plus" />
<!-- <i class="fal fa-minus" /> -->
</div>
<div class="accordion-body">
<div class="accordion-content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. <strong>Pellentesque risus mi</strong></div>
</div>
</div>
<div class="accordion" :class="accordionClasses">
<div class="accordion-header" @click="toggleAccordion">
Accordion 3
<i class="fal fa-plus" />
<!-- <i class="fal fa-minus" />-->
</div>
<div class="accordion-body">
<div class="accordion-content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. <strong>Pellentesque risus mi</strong></div>
</div>
</div>
</div>

js:

export default {
name: 'trial-page',
data() {
return {
isOpen: false
};
},
computed: {
accordionClasses: function() {
return {
'is-closed': !this.isOpen
};
}
},
methods: {
toggleAccordion: function() {
this.isOpen = !this.isOpen;
}
}
};

最佳答案

如果您想跟踪哪个 Accordion 部分是打开的还是关闭的,您需要给每个部分一个索引并将其用作跟踪工具。在创建列表时,我强烈建议您使用 v-for

总的来说,您的代码可以进行一些重构,因此我稍微重写了它,以举例说明您的目标:

<template>
<div id="accordion" class="accordion-container">
<div
v-for="(item, index) in items"
:key="index"
:class="[
'accordion',
{ 'is-open': isOpen.includes(index) }
]"
>
<div class="accordion-header" @click="toggleAccordion(index)">
{{ item.title }}
<i v-if="!isOpen.includes(index)" class="fal fa-plus"/>
<i v-else class="fal fa-minus"/>
</div>
<div class="accordion-body">
<div class="accordion-content">
{{ item.text }}
<strong>{{ item.sub }}</strong>
</div>
</div>
</div>
</div>
</template>

<script>
export default {
name: "trial-page",
data() {
return {
items: [
{
title: "Accordion 1",
text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
sub: "Pellentesque risus mi"
},
{
title: "Accordion 2",
text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
sub: "Pellentesque risus mi"
},
{
title: "Accordion 3",
text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
sub: "Pellentesque risus mi"
}
],
isOpen: []
};
},
methods: {
toggleAccordion(index) {
if (this.isOpen.includes(index)) {
this.isOpen = this.isOpen.filter(i => i !== index);
return;
}

this.isOpen.push(index);
}
}
};
</script>

<style>
.accordion:not(.is-open) .accordion-body {
display: none;
}
</style>

这里有一个 codepen 来测试它:https://codesandbox.io/s/blazing-water-icpxw

您可能需要调整一些内容以满足您的需要。

关于javascript - 仅切换 Accordion 点击 vue.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59753165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com