经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序下拉菜单效果的实例代码
来源:jb51  时间:2019/5/15 8:37:28  对本文有异议


  1. //wcss
  2. /**DropDownMenu**/
  3. /*总菜单容器*/
  4. .menu {
  5. display: block;
  6. height: 28px;
  7. position: relative;
  8. }
  9. /*一级菜单*/
  10. .menu dt {
  11. font-size: 15px;
  12. float: left;
  13. /*hack*/
  14. width: 33%;
  15. height: 38px;
  16. border-right: 1px solid #d2d2d2;
  17. border-bottom: 1px solid #d2d2d2;
  18. text-align: center;
  19. background-color: #f4f4f4;
  20. color: #5a5a5a;
  21. line-height: 38px;
  22. z-index: 2;
  23. }
  1. /*二级菜单外部容器样式*/
  2. .menu dd {
  3. position: absolute;
  4. width: 100%;
  5. margin-top: 40px;
  6. left: 0;
  7. z-index: -99;
  8. }
  9. /*二级菜单普通样式*/
  10. .menu li {
  11. font-size: 14px;
  12. line-height: 34px;
  13. color: #575757;
  14. height: 34px;
  15. display: block;
  16. padding-left: 8px;
  17. background-color: #fff;
  18. border-bottom: 1px solid #dbdbdb;
  19. }
  20. /*二级菜单高亮样式*/
  21. .menu li.highlight {
  22. background-color: #f4f4f4;
  23. color: #48c23d;
  24. }
  25. /* 显示与隐藏 */
  26. .show {
  27. /*display: block;*/
  28. visibility: visible;
  29. }
  30. .hidden {
  31. /*display: none;*/
  32. visibility: hidden;
  33. }
  34. //wxml
  35. <dl class="menu">
  36. <block wx:for="{{reportData}}" wx:key="idMenu" wx:for-item="menuItem" wx:for-index="idMenu">
  37. <dt data-index="{{idMenu}}" bindtap="tapMainMenu">{{menuItem.reportType}}</dt>
  38. <dd class="{{subMenuDisplay[idMenu]}}" animation="{{animationData[idMenu]}}">
  39. <ul wx:for="{{menuItem.chilItem}}" wx:key="chilItem.ID" wx:for-item="chilItem" wx:for-index="idChil">
  40. <li class="{{subMenuHighLight[idMenu][idChil]}}" bindtap="tapSubMenu" data-index="{{idMenu}}-{{idChil}}">{{chilItem.Name}}</li>
  41. </ul>
  42. <picker class="timePicker" mode="date" value="{{dateValue}}" bindchange="datePickerBindchange" start="1999-01-01" end="2999-12-12"> 时间:{{dateValue}}</picker>
  43. </dd>
  44. </block>
  45. </dl>
  46. //js
  47. //数据源
  48. var ReportDataSync = [
  49. {
  50. reportType: "日报1",
  51. chilItem: [
  52. { ID: 1, Name: "日报1", ReportUrl: "DailyReport.aspx", Type: 1 },
  53. { ID: 2, Name: "日报2", ReportUrl: "DailyReport.aspx", Type: 1 },
  54. { ID: 3, Name: "日报3", ReportUrl: "DailyReport.aspx", Type: 1 }]
  55. },
  56. {
  57. reportType: "目录2",
  58. chilItem: [
  59. { ID: 1, Name: "目录1", ReportUrl: "DailyReport.aspx", Type: 2 },
  60. { ID: 2, Name: "目录2", ReportUrl: "DailyReport.aspx", Type: 2 },
  61. { ID: 3, Name: "目录3", ReportUrl: "DailyReport.aspx", Type: 2 },
  62. { ID: 4, Name: "目录4", ReportUrl: "DailyReport.aspx", Type: 2 }]
  63. },
  64. {
  65. reportType: "月报3",
  66. chilItem: [
  67. { ID: 1, Name: "月报1", ReportUrl: "DailyReport.aspx", Type: 1 },
  68. { ID: 2, Name: "月报2", ReportUrl: "DailyReport.aspx", Type: 2 }]
  69. }
  70. ]
  71. //定义字段
  72. var initSubMenuDisplay = []
  73. var initSubMenuHighLight = []
  74. var initAnimationData = []
  75. /// 初始化DropDownMenu
  76. loadDropDownMenu()
  77. that.setData({
  78. reportData: ReportDataSync,//菜单数据
  79. subMenuDisplay: initSubMenuDisplay, //一级
  80. subMenuHighLight: initSubMenuHighLight, //二级
  81. animationData: initAnimationData //动画
  82. })
  83. //一级菜单点击
  84. tapMainMenu: function (e) {
  85. //获取当前一级菜单标识
  86. var index = parseInt(e.currentTarget.dataset.index);
  87. //改变显示状态
  88. for (var i = 0; i < initSubMenuDisplay.length; i++) {
  89. if (i == index) {
  90. if (this.data.subMenuDisplay[index] == "show") {
  91. initSubMenuDisplay[index] = 'hidden'
  92. } else {
  93. initSubMenuDisplay[index] = 'show'
  94. }
  95. } else {
  96. initSubMenuDisplay[i] = 'hidden'
  97. }
  98. }
  99. this.setData({
  100. subMenuDisplay: initSubMenuDisplay
  101. })
  102. this.animation(index)
  103. },
  104. //二级菜单点击
  105. tapSubMenu: function (e) {
  106. //隐藏所有一级菜单
  107. //this.setData({
  108. //subMenuDisplay: initSubMenuDisplay()
  109. //});
  110. // 当前二级菜单的标识
  111. var indexArray = e.currentTarget.dataset.index.split('-');
  112. // 删除所在二级菜单样式
  113. for (var i = 0; i < initSubMenuHighLight.length; i++) {
  114. if (indexArray[0] == i) {
  115. for (var j = 0; j < initSubMenuHighLight[i].length; j++) {
  116. initSubMenuHighLight[i][j] = '';
  117. }
  118. }
  119. }
  120. //给当前二级菜单添加样式
  121. initSubMenuHighLight[indexArray[0]][indexArray[1]] = 'highlight';
  122. //刷新样式
  123. this.setData({
  124. subMenuHighLight: initSubMenuHighLight
  125. });
  126. // 设置动画
  127. this.animation(indexArray[0]);
  128. },
  129. //菜单动画
  130. animation: function (index) {
  131. // 定义一个动画
  132. var animation = wx.createAnimation({
  133. duration: 400,
  134. timingFunction: 'linear',
  135. })
  136. // 是显示还是隐藏
  137. var flag = this.data.subMenuDisplay[index] == 'show' ? 1 : -1;
  138. // 使之Y轴平移
  139. animation.translateY(flag * ((initSubMenuHighLight[index].length + 1) * 38)).step();
  140. // 导出到数据,绑定给view属性
  141. var animationStr = animation.export();
  142. // 原来的数据
  143. var animationData = this.data.animationData;
  144. animationData[index] = animationStr;
  145. this.setData({
  146. animationData: animationData
  147. });
  148. }
  149. /// <summary>
  150. /// 初始化DropDownMenu
  151. /// 1.一级目录 initSubMenuDisplay :['hidden']
  152. /// 2.二级目录 initSubMenuHighLight :[['',''],['','','','']]]
  153. /// </summary>
  154. function loadDropDownMenu() {
  155. for (var i = 0; i < ReportDataSync.length; i++) {
  156. //一级目录
  157. initSubMenuDisplay.push('hidden')
  158. //二级目录
  159. var report = []
  160. for (var j = 0; j < ReportDataSync[i].chilItem.length; j++) {
  161. report.push([''])
  162. }
  163. initSubMenuHighLight.push(report)
  164. //动画
  165. initAnimationData.push("")
  166. }
  167. }

总结

以上所述是小编给大家介绍的微信小程序下拉菜单效果的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号