目的:折叠面板默认显示其中一项,利用toggle实现元素的显示和隐藏
例如:页面中有四个可折叠元素,默认元素1显示,其余项目内容隐藏;当点击元素2时,元素2显示,其余项目内容隐藏。
初始效果如图:
- wxml部分代码如下:
<view class='item' wx:for="{
{items}}" wx:key="index">
<view class='title' data-index="{
{index}}" bindtap='panel'>
{
{item.title}}
</view>
<view class='detail' wx:if="{
{showIndex == index}}">{
{item.text}}</view>
</view>
- js部分代码如下:
Page({
/**
* 页面的初始数据
*/
data: {
showIndex: 0, //默认第一个项目显示
items: [{
title: '折叠项目1',
text: '项目1的内容'
}, {
title: '折叠项目2'