效果图:
js文件
Page({ data:{ li:['默认排序','离我最近','价格最低','价格最高'], shownavindex:0 }, // 下拉事件 listmenu: function(e) { if (this.data.openif) { this.setData({ openif: false, shownavindex: 0 }) } else { this.setData({ content: this.data.li, openif: true, shownavindex: e.currentTarget.dataset.nav }) } } })
wxml文件
<view class="title">2.下拉菜单场景小案例</view> <view class="page"> <!--导航内容--> <view class="nav"> <view class="nav-item {{shownavindex == 1? 'active' : ''}}" bindtap="listmenu" data-nav="1"> <view class="content">排序</view> <view class="icon"></view> </view> <view class="nav-item"> <view class="content">时间</view> <vew class="icon"></vew> </view> <view class="nav-item"> <view class="content">价格</view> <vew class="icon"></vew> </view> </view> <!--下拉内容--> <view class="list {{openif ? 'down' : 'up'}} "> <view wx:for="{{content}}"> {{item}} </view> </view> </view>
josn文件
{ "usingComponents": {} }
wxss文件
.page{ overflow: hidden; /*页面溢出隐藏*/ } .nav{ position: relative; z-index: 1; /*页面元素谁覆盖在谁的上面*/ display: flex; flex-direction: row; background: white; } .nav-item{ display: flex; flex: 1; /*几个内容等分屏幕*/ text-align: center; height: 90rpx; align-items: center; justify-content: center; font-size: 30rpx; border: 1px solid gray; } .icon{ border: 10rpx solid transparent; border-top: 10rpx solid gray; margin-left: 12rpx; } .list{ display: none; /*刚开始尚未显示,所以显示none*/ width: 100%; /*overflow-y: scroll;*/ padding: 0 0 0 20rpx; line-height: 100rpx; background: white; } .list view{ border-bottom: 1px solid gray; font-size: 32rpx; } .nav-item.active .content{ /*注意有空格*/ color: skyblue; } .nav-item.active .icon{ border-bottom: 10rpx solid skyblue; border-top: 0; } .down{ display: block; animation: slidown 0.001s ease-in both; /*添加动画*/ } @keyframes slidown{ from{ transform: translateY(-100%); } to{ transform: translateY(0%); } } .up{ display: block; animation: slidup 0.001s ease-in both; } @keyframes slidup{ from{ transform: translateY(0%); } to{ transform: translateY(-100%); } }