在微信小程序中帮我写一个手风琴
时间: 2023-07-13 15:33:54 浏览: 72
好的,手风琴是一个常见的 UI 控件,可以通过小程序的 WXML 和 WXSS 实现。以下是一个简单的手风琴实现示例:
WXML 代码:
```html
<view class="accordion">
<view class="accordion-item" bindtap="toggleAccordion">
<view class="accordion-header">标题1</view>
<view class="accordion-content">内容1</view>
<view class="accordion-arrow"></view>
</view>
<view class="accordion-item" bindtap="toggleAccordion">
<view class="accordion-header">标题2</view>
<view class="accordion-content">内容2</view>
<view class="accordion-arrow"></view>
</view>
<view class="accordion-item" bindtap="toggleAccordion">
<view class="accordion-header">标题3</view>
<view class="accordion-content">内容3</view>
<view class="accordion-arrow"></view>
</view>
</view>
```
WXSS 代码:
```css
.accordion {
display: flex;
flex-direction: column;
}
.accordion-item {
position: relative;
background-color: #fff;
border: 1px solid #ddd;
margin-bottom: 10px;
}
.accordion-header {
padding: 10px;
}
.accordion-content {
padding: 10px;
display: none;
}
.accordion-arrow {
position: absolute;
top: 50%;
right: 10px;
transform: translateY(-50%);
width: 10px;
height: 10px;
border-style: solid;
border-width: 1px 1px 0 0;
transform: rotate(45deg);
border-color: #333;
}
```
JS 代码:
```javascript
Page({
data: {
activeIndex: -1
},
toggleAccordion(e) {
const index = e.currentTarget.dataset.index;
const activeIndex = index === this.data.activeIndex ? -1 : index;
this.setData({
activeIndex: activeIndex
});
}
})
```
实现了一个简单的手风琴,当用户点击手风琴的某个标题时,对应的内容会展开或收起。希望这个代码对你有所帮助!
阅读全文