switchcity.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var city = require('../../utils/city.js')
  2. Page({
  3. data: {
  4. searchLetter: [],
  5. showLetter: "",
  6. winHeight: 0,
  7. tHeight:0,
  8. bHeight:0,
  9. startPageY:0,
  10. cityList:[],
  11. isShowLetter:false,
  12. scrollTop:0,
  13. city:""
  14. },
  15. onLoad: function (options) {
  16. // 生命周期函数--监听页面加载
  17. var searchLetter = city.searchLetter;
  18. var cityList=city.cityList();
  19. // console.log(cityInfo);
  20. var sysInfo = wx.getSystemInfoSync();
  21. console.log(sysInfo);
  22. var winHeight = sysInfo.windowHeight;
  23. //添加要匹配的字母范围值
  24. //1、更加屏幕高度设置子元素的高度
  25. var itemH = winHeight / searchLetter.length;
  26. var tempObj = [];
  27. for (var i = 0; i < searchLetter.length; i++) {
  28. var temp = {};
  29. temp.name = searchLetter[i];
  30. temp.tHeight = i * itemH;
  31. temp.bHeight = (i + 1) * itemH;
  32. tempObj.push(temp)
  33. }
  34. this.setData({
  35. winHeight: winHeight,
  36. itemH: itemH,
  37. searchLetter: tempObj,
  38. cityList:cityList
  39. })
  40. console.log(this.data.cityInfo);
  41. },
  42. onReady: function () {
  43. // 生命周期函数--监听页面初次渲染完成
  44. },
  45. onShow: function () {
  46. // 生命周期函数--监听页面显示
  47. },
  48. onHide: function () {
  49. // 生命周期函数--监听页面隐藏
  50. },
  51. onUnload: function () {
  52. // 生命周期函数--监听页面卸载
  53. },
  54. onPullDownRefresh: function () {
  55. // 页面相关事件处理函数--监听用户下拉动作
  56. },
  57. onReachBottom: function () {
  58. // 页面上拉触底事件的处理函数
  59. },
  60. onShareAppMessage: function () {
  61. // 用户点击右上角分享
  62. return {
  63. title: 'title', // 分享标题
  64. desc: 'desc', // 分享描述
  65. path: 'path' // 分享路径
  66. }
  67. },
  68. searchStart: function (e) {
  69. var showLetter = e.currentTarget.dataset.letter;
  70. var pageY = e.touches[0].pageY;
  71. this.setScrollTop(this,showLetter);
  72. this.nowLetter(pageY,this);
  73. this.setData({
  74. showLetter: showLetter,
  75. startPageY: pageY,
  76. isShowLetter:true,
  77. })
  78. },
  79. searchMove: function (e) {
  80. var pageY = e.touches[0].pageY;
  81. var startPageY=this.data.startPageY;
  82. var tHeight=this.data.tHeight;
  83. var bHeight=this.data.bHeight;
  84. var showLetter = 0;
  85. console.log(pageY);
  86. if(startPageY-pageY>0){ //向上移动
  87. if(pageY<tHeight){
  88. // showLetter=this.mateLetter(pageY,this);
  89. this.nowLetter(pageY,this);
  90. }
  91. }else{//向下移动
  92. if(pageY>bHeight){
  93. // showLetter=this.mateLetter(pageY,this);
  94. this.nowLetter(pageY,this);
  95. }
  96. }
  97. },
  98. searchEnd: function (e) {
  99. // console.log(e);
  100. // var showLetter=e.currentTarget.dataset.letter;
  101. var that=this;
  102. setTimeout(function(){
  103. that.setData({
  104. isShowLetter:false
  105. })
  106. },1000)
  107. },
  108. nowLetter: function (pageY, that) { //当前选中的信息
  109. var letterData = this.data.searchLetter;
  110. var bHeight = 0;
  111. var tHeight = 0;
  112. var showLetter="";
  113. for (var i = 0; i < letterData.length; i++) {
  114. if (letterData[i].tHeight <= pageY && pageY<= letterData[i].bHeight) {
  115. bHeight = letterData[i].bHeight;
  116. tHeight = letterData[i].tHeight;
  117. showLetter = letterData[i].name;
  118. break;
  119. }
  120. }
  121. this.setScrollTop(that,showLetter);
  122. that.setData({
  123. bHeight:bHeight,
  124. tHeight:tHeight,
  125. showLetter:showLetter,
  126. startPageY:pageY
  127. })
  128. },
  129. bindScroll:function(e){
  130. console.log(e.detail)
  131. },
  132. setScrollTop:function(that,showLetter){
  133. var scrollTop=0;
  134. var cityList=that.data.cityList;
  135. var cityCount=0;
  136. var initialCount=0;
  137. for(var i=0;i<cityList.length;i++){
  138. if(showLetter==cityList[i].initial){
  139. scrollTop=initialCount*30+cityCount*41;
  140. break;
  141. }else{
  142. initialCount++;
  143. cityCount+=cityList[i].cityInfo.length;
  144. }
  145. }
  146. that.setData({
  147. scrollTop:scrollTop
  148. })
  149. },
  150. bindCity:function(e){
  151. var city=e.currentTarget.dataset.city;
  152. this.setData({city:city})
  153. }
  154. })