微信小程序中根据字母选择城市

本文档详细介绍了如何在微信小程序中开发一个根据字母选择城市的特性。通过创建index.wxml、index.wxss、index.js文件及城市数据文件city.js,实现城市选择功能。适合小程序开发者参考学习。
摘要由CSDN通过智能技术生成

今天开发一个小程序,里面涉及到区域选择,看了网上的一些版本,感觉写的不全,有可能是我的理解能力还不够吧。今天我就结合网上的答案,在根据自己的需求,重新整理一份。希望对大家有帮助。先看看截图:

项目截图 
下面我们把代码梳理一下。

一、创建index.wxml文件

在pages->index文件夹下,新建index.wxml文件,代码如下:

 
 
 
  1. <view< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> class="title">
  2. <input< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> class="title_list" value="{ {cityName}}" placeholder="城市名称" />
  3. <scroll-view< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> scroll-y="true" style="height: { { winHeight}}px;" scroll-into-view="{ {scrollTopId}}" class="city_list">
  4. <block< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> wx:for="{ {city}}" wx:for-index="idx" wx:for-item="cityName">
  5. <text< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> id="{ {idx}}" class="list_tit">{ {idx}}
  6. <block< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> wx:for="{ {cityName}}">
  7. <view< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> class="list_con" data-city="{ {item.name}}" bindtap="bindCity">{ {item.name}}
  8. <view< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> class="scroll_list"
  9. bindtouchstart="chStart"
  10. bindtouchend="chEnd"
  11. catchtouchmove="chMove"
  12. style="background: rgba(0,0,0,{ { trans}});"
  13. >
  14. <block< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> wx:for="{ {city}}" wx:for-index="idx" wx:for-item="cityName">
  15. <block< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> wx:if="{ {idx != '热门城市'}}">
  16. <view< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> id="{ {idx}}" class="scroll_list_chi" style="line-height:{ { lineHeight}}px ; height:{ { lineHeight}}px ;font-size:{ { lineHeight/1.7}}px;" bindtouchstart="getWords" bindtouchend="setWords">{ {idx}}
  17. <view< span="" style="word-wrap: break-word; margin: 0px; padding: 0px;"> hidden="{ {hidden}}" class="showwords">
  18. { {showwords}}

二、创建对应的CSS

在pages->index文件夹下,新建index.wxss文件,代码如下:

 
 
 
  1. /**index.wxss**/
  2. .title {
  3. position: relative;
  4. padding: 10px 0;
  5. }
  6. .title_list {
  7. display: inline-block;
  8. padding: 0 15px;
  9. height: 20px;
  10. line-height: 20px;
  11. font-size: 16px;
  12. }
  13. .title button {
  14. width: 50px;
  15. height: 30px;
  16. font-size: 16px;
  17. padding: 0;
  18. line-height: 30px;
  19. margin: auto;
  20. position: absolute;
  21. top: 0;
  22. bottom:0;
  23. right: 10px;
  24. background: none;
  25. }
  26. .title button::after {
  27. border: none;
  28. }
  29. .title_list:nth-child(1) {
  30. border-right:1px #ccc solid;
  31. }
  32. /*城市列表*/
  33. .city_list {
  34. position: relative;
  35. }
  36. /*城市选择头部*/
  37. .list_tit {
  38. display: block;
  39. line-height: 40px;
  40. height: 40px;
  41. padding-left: 15px;
  42. font-size: 16ppx;
  43. background: #f5f5f5;
  44. color: #666;
  45. }
  46. .list_con {
  47. height: 40px;
  48. /*border-top: 1px #f5f5f5 solid ;*/
  49. line-height: 40px;
  50. font-size: 16px;
  51. padding-left: 15px;
  52. }
  53. .list_con::before {
  54. content: " ";
  55. height: 1px;
  56. border-top: 1px #f5f5f5 solid;
  57. position: absolute;
  58. width: 100%;
  59. }
  60. .list_con::before:nth-child(1) {
  61. border: none;
  62. }
  63. /*城市选择 右边*/
  64. .scroll_list {
  65. background: rgba(0,0,0,0);
  66. position: absolute;
  67. height: calc(100% - 100px);
  68. width: 25px;
  69. top: 90px;
  70. right: 10px;
  71. }
  72. .scroll_list_chi {
  73. /*border: 1px blue solid;*/
  74. text-align: center;
  75. font-size: 12px;
  76. }
  77. /*显示框*/
  78. .showwords {
  79. width: 80px;
  80. height: 80px;
  81. background: rgba(0,0,0,.3);
  82. border-radius:50%;
  83. line-height: 80px;
  84. text-align: center;
  85. font-size:10vw;
  86. margin: auto;
  87. position: absolute;
  88. top: 0;left: 0;bottom: 0;right: 0;
  89. z-index: 999;
  90. }

三、创建JS文件

在pages->index文件夹下,新建index.js文件,代码如下:

 
 
 
  1. //先引用城市数据文件
  2. var city = require('../../utils/city.js')
  3. var lineHeight = 0;
  4. var endWords = "";
  5. var isNum;
  6. Page({
  7. data: {
  8. "hidden": true,
  9. cityName:"", //获取选中的城市名
  10. },
  11. onLoad: function (options) {
  12. // 生命周期函数--监听页面加载
  13. },
  14. onReady: function () {
  15. // 生命周期函数--监听页面初次渲染完成
  16. var cityChild = city.City[0];
  17. var that = this;
  18. wx.getSystemInfo({
  19. success: function (res) {
  20. lineHeight = (res.windowHeight - 100) / 22;
  21. console.log(res.windowHeight - 100)
  22. that.setData({
  23. city: cityChild,
  24. winHeight: res.windowHeight - 40,
  25. lineHeight: lineHeight
  26. })
  27. }
  28. })
  29. },
  30. onShow: function () {
  31. // 生命周期函数--监听页面显示
  32. },
  33. onHide: function () {
  34. // 生命周期函数--监听页面隐藏
  35. },
  36. onUnload: function () {
  37. // 生命周期函数--监听页面卸载
  38. },
  39. //触发全部开始选择
  40. chStart: function () {
  41. this.setData({
  42. trans: ".3",
  43. hidden: false
  44. })
  45. },
  46. //触发结束选择
  47. chEnd: function () {
  48. this.setData({
  49. trans: "0",
  50. hidden: true,
  51. scrollTopId: this.endWords
  52. })
  53. },
  54. //获取文字信息
  55. getWords: function (e) {
  56. var id = e.target.id;
  57. this.endWords = id;
  58. isNum = id;
  59. this.setData({
  60. showwords: this.endWords
  61. })
  62. },
  63. //设置文字信息
  64. setWords: function (e) {
  65. var id = e.target.id;
  66. this.setData({
  67. scrollTopId: id
  68. })
  69. },
  70. // 滑动选择城市
  71. chMove: function (e) {
  72. var y = e.touches[0].clientY;
  73. var offsettop = e.currentTarget.offsetTop;
  74. var height = 0;
  75. var that = this;
  76. ;
  77. var cityarr = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"]
  78. // 获取y轴最大值
  79. wx.getSystemInfo({
  80. success: function (res) {
  81. height = res.windowHeight - 10;
  82. }
  83. });
  84. //判断选择区域,只有在选择区才会生效
  85. if (y > offsettop && y < height) {
  86. // console.log((y-offsettop)/lineHeight)
  87. var num = parseInt((y - offsettop) / lineHeight);
  88. endWords = cityarr[num];
  89. // 这里 把endWords 绑定到this 上,是为了手指离开事件获取值
  90. that.endWords = endWords;
  91. };
  92. //去除重复,为了防止每次移动都赋值 ,这里限制值有变化后才会有赋值操作,
  93. //DOTO 这里暂时还有问题,还是比较卡,待优化
  94. if (isNum != num) {
  95. // console.log(isNum);
  96. isNum = num;
  97. that.setData({
  98. showwords: that.endWords
  99. })
  100. }
  101. },
  102. //选择城市,并让选中的值显示在文本框里
  103. bindCity: function(e) {
  104. console.log(e);
  105. var cityName = e.currentTarget.dataset.city;
  106. this.setData({ cityName: cityName })
  107. }
  108. })

四、创建城市文件

在utils文件夹里创建city.js文件,具体代码如下:

 
 
 
  1. var city = {
  2. "City": [
  3. {
  4. "热门城市": [
  5. {
  6. "name": "北京",
  7. "key": "热门"
  8. },
  9. {
  10. "name": "上海",
  11. "key": "热门"
  12. },
  13. {
  14. "name": "广州",
  15. "key": "热门"
  16. },
  17. {
  18. "name": "深圳",
  19. "key": "热门"
  20. },
  21. {
  22. "name": "成都",
  23. "key": "热门"
  24. },
  25. {
  26. "name": "重庆",
  27. "key": "热门"
  28. },
  29. {
  30. "name": "天津",
  31. "key": "热门"
  32. },
  33. {
  34. "name": "杭州",
  35. "key": "热门"
  36. },
  37. {
  38. "name": "南京",
  39. "key": "热门"
  40. },
  41. {
  42. "name": "苏州",
  43. "key": "热门"
  44. },
  45. {
  46. "name": "武汉",
  47. "key": "热门"
  48. },
  49. {
  50. "name": "西安",
  51. "key": "热门"
  52. }
  53. ],
  54. "A": [
  55. {
  56. "name": "阿坝",
  57. "key": "A"
  58. },
  59. {
  60. "name": "阿拉善",
  61. "key": "A"
  62. },
  63. {
  64. "name": "阿里",
  65. "key": "A"
  66. },
  67. {
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值