<"> <"> 小程序点击菜单动态切换class
 个人随笔
目录
小程序点击菜单动态切换class
2019-03-31 21:35:04

弄了半天,都比较难以实现类似随笔博客首页菜单切换的样式,因为不知道js中如何获取标签,然后只能用数据绑定的方式来解决,如下代码,当然只是一个凌乱的例子:

1、sb.wxml

  1. <!--pages/sb/sb.wxml-->
  2. <view class="top">
  3. <view class="tops {{new==1?'choice':'nochoice'}}" bindtap="getNewTopics"><text>最新</text></view>
  4. <view class="top_span tops"></view>
  5. <view class="tops {{month==1?'choice':'nochoice'}}" bindtap="getMonthTopics"><text>月榜</text></view>
  6. <view class="top_span tops"></view>
  7. <view class="tops {{year==1?'choice':'nochoice'}}" bindtap="getYearTopics"><text>年榜</text></view>
  8. <view class="top_span tops"></view>
  9. <view class="tops {{user==1?'choice':'nochoice'}}" bindtap="getHotUsers"><text>博主</text></view>
  10. <view class="top_span tops"></view>
  11. <view class="tops {{menu==1?'choice':'nochoice'}}" bindtap="getHotMenus"><text>标签</text></view>
  12. </view>

2、sb.wxss

  1. /* pages/sb/sb.wxss */
  2. .top{
  3. font-size: 14px;
  4. width: 100%;
  5. text-align: center;
  6. }
  7. .tops{
  8. display: inline-block;
  9. color: #231815;
  10. padding-bottom: 3px;
  11. }
  12. .top_span{
  13. margin-left: 5px;
  14. margin-right: 5px;
  15. width: 0px;
  16. }
  17. .choice {
  18. color: #231815;
  19. border-bottom: 1px solid #333;
  20. padding-bottom: 2px;
  21. }
  22. .nochoice {
  23. color: #A3A3A3;
  24. border-bottom: 0px solid #333;
  25. }

3、sb.js

  1. // pages/sb/sb.js
  2. Page({
  3. data: {
  4. motto: 'Hello World',
  5. new: 1
  6. },
  7. //事件处理函数
  8. getNewTopics: function () {
  9. this.setData({
  10. new: 1,
  11. month:0,
  12. year:0,
  13. user:0,
  14. menu:0
  15. });
  16. // wx.request({
  17. // url: "https://www.suibibk.com/getTopics",
  18. // data: {menuid:0,userid:0,page:1,hot:"",type:0},
  19. // method:"POST",
  20. // header: {
  21. // "Content-Type":"application/json"
  22. // },
  23. // success: function (res) {
  24. // console.log(res.data)
  25. // },
  26. // fail: function (err) {
  27. // console.log(err)
  28. // }
  29. // })
  30. },
  31. //事件处理函数
  32. getMonthTopics: function () {
  33. this.setData({
  34. new: 0,
  35. month: 1,
  36. year: 0,
  37. user: 0,
  38. menu: 0
  39. });
  40. },
  41. //事件处理函数
  42. getYearTopics: function () {
  43. this.setData({
  44. new: 0,
  45. month: 0,
  46. year: 1,
  47. user: 0,
  48. menu: 0
  49. });
  50. },
  51. //事件处理函数
  52. getHotUsers: function () {
  53. this.setData({
  54. new: 0,
  55. month: 0,
  56. year: 0,
  57. user: 1,
  58. menu: 0
  59. });
  60. },
  61. //事件处理函数
  62. getHotMenus: function () {
  63. this.setData({
  64. new: 0,
  65. month: 0,
  66. year: 0,
  67. user: 0,
  68. menu: 1
  69. });
  70. }
  71. })

4、sb.json

  1. {
  2. "navigationBarTitleText": "随笔小博客",
  3. "component": true,
  4. "usingComponents": {}
  5. }

5、运行实例,如下呈现

总结

不知道啥时候可以开启小程序的学习。

 444

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2