最近写的小程序里面需要实现顶部下拉菜单的效果,做一个过滤操作,但是没有找到相关组件,所以动手写了一个。

先看一下这拙劣的效果叭~



下面就直接看具体实现了嗷!

index.wxml

  1. <view class="contain">
  2. <view class="select_box" wx:for="{{ selectList }}" wx:key="">
  3. <view class="select_tab {{ showIndex == index ? 'active' : '' }} {{item.active?'active':''}}" data-id="{{index}}" bindtap="chooseTab">
  4. <view>{{ item.active || item.name }}</view>
  5. <img src="../../images/arrow_down.png" class="arrow" wx:if="{{ showIndex != index }}">
  6. <img src="../../images/arrow_up.png" class="arrow" wx:if="{{ showIndex == index }}">
  7. </view>
  8. <scroll-view scroll-y="" class="option_list {{ showIndex == index ? 'slidedown' : 'slideup'}}">
  9. <view wx:for="{{ item.list }}" wx:for-item="val" wx:for-index="idx" wx:key="val.id" class="option_item {{item.active == val.content?'active_option':''}}" data-index="{{index}}" data-value="{{val.content}}" bindtap="chooseOption">{{ val.content }}</view>
  10. </scroll-view>
  11. </view>
  12. </view>

index.wxss

  1. page{
  2. background-color: #fafafa;
  3. }
  4. .contain{
  5. display: flex;
  6. }
  7. .select_box{
  8. line-height: 80rpx;
  9. }
  10. .select_tab{
  11. width: 250rpx;
  12. background-color: #fff;
  13. display: flex;
  14. justify-content: center;
  15. align-items: center;
  16. position: relative;
  17. z-index: 20;
  18. }
  19. .option_list{
  20. width: 250rpx;
  21. background-color: #fff;
  22. position: absolute;
  23. z-index: 10;
  24. }
  25. .option_item{
  26. color: #888;
  27. text-align: center;
  28. }
  29. .arrow{
  30. width: 45rpx;
  31. height: 45rpx;
  32. }
  33. .active{
  34. color: #1296db;
  35. }
  36. .active_option{
  37. color: #1296db;
  38. background-color: #e5f2ff;
  39. }
  40. .tips{
  41. line-height: 2;
  42. background-color: #fff;
  43. margin-top: 50rpx;
  44. padding: 40rpx;
  45. }
  46. /* 动画效果 */
  47. @keyframes slideup {
  48. from {
  49. max-height: 600rpx;
  50. }
  51. to {
  52. max-height: 0;
  53. }
  54. }
  55. .slideup {
  56. animation: slideup 0.2s ease-in both;
  57. }
  58. @keyframes slidedown {
  59. from {
  60. max-height: 0;
  61. }
  62. to {
  63. max-height: 600rpx;
  64. }
  65. }
  66. .slidedown {
  67. animation: slidedown 0.2s ease-in both;
  68. }

index.js

  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. selectList: [
  7. {
  8. name: 'select-1',
  9. list: [
  10. { id: 1, content: '选项1' },
  11. { id: 1, content: '选项2' },
  12. { id: 1, content: '选项3' },
  13. { id: 1, content: '选项4' },
  14. { id: 1, content: '选项5' },
  15. { id: 1, content: '选项6' },
  16. { id: 1, content: '选项7' },
  17. { id: 1, content: '选项8' }
  18. ],
  19. },
  20. {
  21. name: 'select-2',
  22. list: [
  23. { id: 1, content: '选项1' },
  24. { id: 1, content: '选项2' },
  25. { id: 1, content: '选项3' },
  26. { id: 1, content: '选项4' },
  27. { id: 1, content: '选项5' },
  28. { id: 1, content: '选项6' },
  29. { id: 1, content: '选项7' },
  30. { id: 1, content: '选项8' },
  31. { id: 1, content: '选项9' },
  32. { id: 1, content: '选项10' }
  33. ]
  34. },
  35. {
  36. name: 'select-3',
  37. list: [
  38. { id: 1, content: '选项1' },
  39. { id: 1, content: '选项2' },
  40. { id: 1, content: '选项3' },
  41. { id: 1, content: '选项4' },
  42. { id: 1, content: '选项5' },
  43. { id: 1, content: '选项6' },
  44. { id: 1, content: '选项7' },
  45. { id: 1, content: '选项8' },
  46. { id: 1, content: '选项9' },
  47. { id: 1, content: '选项10' },
  48. { id: 1, content: '选项11' },
  49. { id: 1, content: '选项12' },
  50. { id: 1, content: '选项13' },
  51. { id: 1, content: '选项14' }
  52. ]
  53. }
  54. ],
  55. showIndex: -1,
  56. },
  57. // 选中select_tab
  58. chooseTab(e){
  59. let index = e.currentTarget.dataset.id;
  60. if(index !== this.data.showIndex){
  61. this.setData({
  62. showIndex: index
  63. })
  64. }else{
  65. // 再次点击应该收起
  66. this.setData({
  67. showIndex: -1
  68. })
  69. }
  70. },
  71. // 选中选项
  72. chooseOption(e){
  73. let val = e.currentTarget.dataset.value,
  74. idx = e.currentTarget.dataset.index;
  75. this.setData({
  76. [`selectList[${idx}].active`]: val,
  77. showIndex: -1
  78. });
  79. }
  80. })

完毕!希望有帮助,有问题请多多指出,菜鸡将不胜感激~

微信小程序-顶部下拉菜单实现的更多相关文章

  1. 微信小程序禁止下拉_解决小程序下拉出现空白的情况

    微信小程序禁止下拉 在微信小程序中,用力往下拉动,页面顶部会出现一段空白的地方. 产品的需求不太允许这么做,会影响用户体验,查看文档发现可以使用enablePullDownRefresh这属性来实现, ...

  2. 微信小程序的下拉刷新

    微信小程序的下拉刷新:在page的js文件中有监听用户下拉刷新的处理函数onPullDownRefresh:function(){} //js文件中自带的处理函数,在onUnload下面,注意不要重复 ...

  3. 小程序多级下拉菜单demo

    小程序多级下拉菜单demo - CSDN博客 https://blog.csdn.net/github_39371177/article/details/80251211

  4. 微信小程序-自定义下拉刷新

    最近给别个公司做技术支持,要实现微信小程序上拉刷新与下拉加载更多 微信给出的接口不怎么友好,最终想实现效果类似QQ手机版 ,一共3种下拉刷新状态变化,文字+图片+背景颜色 最终实现后的效果(这里提示有 ...

  5. 微信小程序-页面下拉

    微信小程序当滑动到最顶部和最底部时,继续下拉,会将整个页面拉下去或者拉上去,本来以为是客户端自有的特性,就没去管他,直到我的禅道出现了这个记录... 其实这个问题是可以解决的,只需要在你不想出现在此情 ...

  6. 微信小程序开发之下拉菜单

    实现功能:点击维保人员,调出下拉菜单.选择子菜单时,显示右边的图标表示选中,并进行赋值并进行搜索筛选 Wxml: <view class="dtclass" bindtap= ...

  7. 微信小程序iOS下拉白屏晃动,坑坑坑

    感觉ios的小程序每个页面都可以下拉出现白屏 有时页面带有滑动的属性会跟着晃动,体验不是很好 解决办法: 先禁止页面下拉 <config> { navigationBarTitleText ...

  8. 微信小程序:下拉刷新

    下拉刷新 1.需要在json文件中,设置"enablePullDownRefresh": true,表示该页面使用下拉刷新 2.在微信内置函数onPullDownRefresh中进 ...

  9. 【微信小程序】下拉刷新真机测试无效

    根据文档的描述,做上拉加载时直接实现页面的onReachBottom()函数即可.但是要做下拉刷新时,除了实现onPullDownRefresh()函数外,还必须要在app.json中配置开启enab ...

  10. 微信小程序picker下拉绑定数据

    页面部分 <picker mode = "selector" bindchange="bindPickerChange" value="{{pr ...

随机推荐

  1. 每日一抄 Go语言聊天服务器

    server.go package main import ( "bufio" "fmt" "log" "net" ) ...

  2. 安装filebeat

    Filebeat是本地文件的日志数据采集器. 作为服务器上的代理安装,Filebeat监视日志目录或特定日志文件,tail file,并将它们转发给Elasticsearch或Logstash进行索引 ...

  3. Jmeter添加JSR223对Python的支持

    通过下载:org.python : jython-standalone : 2.7.2 - Maven Central Repository Search jython-standalone-2.7. ...

  4. cpu主频对网络传输性能的影响

    数据包长度是:2KB iperf的测试结果:3.2GHz的cpu能上40Gb/s, 2.1GHz的cpu只能到28Gb/s.

  5. rdlc报表需要显示的时间格式

    [仅取当前时间的年月日]=FormatDateTime(System.DateTime.Now.ToLongDateString().ToString()) [仅取数据库保存的时间的年月日]=form ...

  6. vs2019 debug 出现: printf is ambiguous

    在vs中写c++代码时,莫名其妙出现:printf is ambiguous 的错误. 第一步,有设置std namespace 删除后再输入 using namespace std; 第二步.删除u ...

  7. php curl 模拟post提交

    /** * PHP发送Json对象数据 * @param $url 请求url * @param $jsonStr 发送的json字符串 * @return array */public functi ...

  8. jetson nano 4gb记录

    常用命令 Ctrl alt t 打开终端gnome-session-properties 打开开机自启动管理界面 ifconfig 查看ip xrandr --fb 1200x800 调节分辨率 ll ...

  9. Nginx 代理解决跨域问题分析

    Nginx 代理解决跨域问题分析   当你遇到跨域问题,不要立刻就选择复制去尝试.请详细看完这篇文章再处理 .我相信它能帮到你. 分析前准备: 前端网站地址:http://localhost:8080 ...

  10. vue-固定头部-内容可滚动

     <div class="show-box">             <div class="show-top">           ...