city.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // pages/city/city.js
  2. import {
  3. cities
  4. } from './json/cites' // 你的路径
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. citys: cities,
  11. apl_index: 0,
  12. offTop: 0,
  13. searchCity: '',
  14. searchCityList: []
  15. },
  16. jumpTo: function (e) {
  17. // 获取标签元素上自定义的 data-opt 属性的值
  18. let target = e.currentTarget.dataset.opt;
  19. let index = e.currentTarget.dataset.index;
  20. this.setData({
  21. toView: target,
  22. apl_index: index
  23. })
  24. },
  25. getAplPos() {
  26. this.setData({
  27. apl_index: 0
  28. })
  29. wx.createSelectorQuery().select('.scrollBox').boundingClientRect((res) => {
  30. console.log(res)
  31. this.setData({
  32. offTop: res.top
  33. })
  34. let tab = this.data.citys
  35. let arr = []
  36. for (let i = 0; i < tab.length; i++) {
  37. wx.createSelectorQuery().select('#item' + i).boundingClientRect((res_i) => {
  38. // console.log(res_i)
  39. tab[i].top = res_i.top - this.data.offTop;
  40. this.setData({
  41. citys: tab,
  42. // apl_posArr: arr
  43. })
  44. }).exec()
  45. }
  46. }).exec()
  47. },
  48. onPageScroll(e) {
  49. //创建节点选择器
  50. let query = wx.createSelectorQuery();
  51. //选择id
  52. query.select('.scrollBox').boundingClientRect((rect) => {
  53. let scrollTop = e.detail.scrollTop
  54. let tabs = this.data.citys
  55. wx.showToast({
  56. title: tabs[this.data.apl_index].title,
  57. duration: 300,
  58. icon: 'none'
  59. })
  60. for (let i = 0; i < tabs.length; i++) {
  61. if (scrollTop > tabs[i].top && scrollTop < tabs[i + 1].top) {
  62. this.setData({
  63. apl_index: i
  64. })
  65. } else if (scrollTop >= tabs[tabs.length - 1].top) {
  66. // console.log(1)
  67. this.setData({
  68. apl_index: tabs.length - 1
  69. })
  70. return
  71. }
  72. }
  73. }).exec();
  74. },
  75. search() {
  76. if (this.data.searchCity != '') {
  77. // console.log(this.data.searchCity)
  78. let arr = []
  79. for (let i = 0; i < this.data.citys.length; i++) {
  80. this.data.citys[i].lists.filter((res) => {
  81. if (res.includes(this.data.searchCity)) {
  82. arr.push(res);
  83. this.setData({
  84. searchCityList: arr
  85. })
  86. }
  87. })
  88. }
  89. if (arr.length == 0) {
  90. wx.showToast({
  91. title: '查无此城市',
  92. icon: 'none'
  93. })
  94. } else {
  95. }
  96. } else {
  97. this.setData({
  98. searchCityList: []
  99. })
  100. if (this.data.searchCityList.length == 0) {
  101. wx.showToast({
  102. title: '请输入城市名称',
  103. icon: 'none'
  104. })
  105. }
  106. }
  107. },
  108. inputChange(e) {
  109. this.setData({
  110. searchCity: e.detail.value,
  111. });
  112. },
  113. changeCity(e) {
  114. let city = e.target.dataset.city
  115. wx.setStorageSync('city', city)
  116. getApp().globalData.city = city
  117. wx.navigateBack()
  118. },
  119. /**
  120. * 生命周期函数--监听页面加载
  121. */
  122. onLoad: function (options) {
  123. // console.log(cities)
  124. },
  125. /**
  126. * 生命周期函数--监听页面初次渲染完成
  127. */
  128. onReady: function () {
  129. },
  130. /**
  131. * 生命周期函数--监听页面显示
  132. */
  133. onShow: function () {
  134. this.getAplPos()
  135. },
  136. /**
  137. * 生命周期函数--监听页面隐藏
  138. */
  139. onHide: function () {
  140. },
  141. /**
  142. * 生命周期函数--监听页面卸载
  143. */
  144. onUnload: function () {
  145. },
  146. /**
  147. * 页面相关事件处理函数--监听用户下拉动作
  148. */
  149. onPullDownRefresh: function () {
  150. },
  151. /**
  152. * 页面上拉触底事件的处理函数
  153. */
  154. onReachBottom: function () {
  155. },
  156. /**
  157. * 用户点击右上角分享
  158. */
  159. onShareAppMessage: function () {
  160. }
  161. })