index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. zimu_list: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
  7. scroll: '', //滚动到指定 id值的子元素
  8. touchmove: 0,//给字母导航添加背景色 1 添加 0不添加
  9. nav_height: '',//字母导航单个元素高度
  10. nav_top:'',
  11. hiddenn: true,//hint_box 提示框 展示隐藏
  12. nav_text: '',//hint_box 提示框里面的文本
  13. lb:[
  14. {
  15. url:'',
  16. text: '中国科学家实现“量子计算优越性”里程碑'
  17. },
  18. {
  19. url: '',
  20. text: '中国科学家实现“量子计算优越性”里程碑'
  21. },
  22. {
  23. url: '',
  24. text: '中国科学家实现“量子计算优越性”里程碑'
  25. }
  26. ]
  27. },
  28. onReady: function () {
  29. this.get_height();
  30. },
  31. //事件处理函数
  32. touchstart: function (e) {
  33. this.set_scroll(e)
  34. },
  35. touchmove: function (e) {
  36. this.set_scroll(e)
  37. },
  38. touchend: function () {
  39. this.setData({
  40. touchmove: 0,
  41. hiddenn: true
  42. })
  43. },
  44. scroll(e){
  45. var that = this
  46. this.setData({
  47. scroll:e.target.dataset.id.toLowerCase(),
  48. })
  49. },
  50. set_scroll: function (e) {
  51. var page_y = e.changedTouches[0].pageY - this.data.nav_top;
  52. // console.log(page_y)
  53. var nav_height = +this.data.nav_height
  54. var idx = Math.floor(page_y / nav_height)
  55. var zimu = this.data.zimu_list[idx];
  56. this.setData({
  57. touchmove: 1,
  58. scroll: zimu.toLowerCase(),
  59. nav_text: zimu,
  60. hiddenn: false
  61. })
  62. },
  63. get_height: function () {
  64. var that = this
  65. var query = wx.createSelectorQuery();
  66. query.select('#nav_item').boundingClientRect()
  67. wx.createSelectorQuery().select('#nav').boundingClientRect(function (rect) {
  68. that.setData({
  69. nav_top: rect.top,
  70. })
  71. }).exec()
  72. query.exec(function (res) {
  73. that.setData({
  74. nav_height: res[0].height,
  75. })
  76. })
  77. },
  78. onLoad: function () {
  79. if (app.globalData.userInfo) {
  80. this.setData({
  81. userInfo: app.globalData.userInfo,
  82. hasUserInfo: true
  83. })
  84. } else if (this.data.canIUse){
  85. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  86. // 所以此处加入 callback 以防止这种情况
  87. app.userInfoReadyCallback = res => {
  88. this.setData({
  89. userInfo: res.userInfo,
  90. hasUserInfo: true
  91. })
  92. }
  93. } else {
  94. // 在没有 open-type=getUserInfo 版本的兼容处理
  95. wx.getUserInfo({
  96. success: res => {
  97. app.globalData.userInfo = res.userInfo
  98. this.setData({
  99. userInfo: res.userInfo,
  100. hasUserInfo: true
  101. })
  102. }
  103. })
  104. }
  105. },
  106. getUserInfo: function(e) {
  107. console.log(e)
  108. app.globalData.userInfo = e.detail.userInfo
  109. this.setData({
  110. userInfo: e.detail.userInfo,
  111. hasUserInfo: true
  112. })
  113. }
  114. })