index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <view class="select-city-wrap" :style="'height:'+windowHeight">
  3. <!-- 内容 -->
  4. <scroll-view class="scroll" :scroll-into-view="scrollIntoId" :scroll-y="true" :scroll-with-animation="true"
  5. :show-scrollbar="false" :style="'height:'+windowHeight">
  6. <view class="action" @tap="BackPage">
  7. <view class="action-box">
  8. <text class="cuIcon-back"></text>
  9. <slot name="backText">选择城市</slot>
  10. <text style="width:72rpx"></text>
  11. </view>
  12. </view>
  13. <!-- 当前城市 -->
  14. <view style="height:200rpx">
  15. <text class="name" id="current">当前城市</text>
  16. <view class="city-item" v-if="city">
  17. <view class="city-item-view">
  18. <text class="city-item-text">{{ city }}</text>
  19. </view>
  20. </view>
  21. </view>
  22. <!-- 热门城市 -->
  23. <text class="name">热门城市</text>
  24. <view class="city-item">
  25. <view :class=" city== cityname?'city-item-viewa':'city-item-view' " v-for="(cityname, i) in hotCitys"
  26. :key="cityname + 4" @click="onSelect(cityname)">
  27. <text :class=" city== cityname?'city-item-texta':'city-item-text' ">{{ cityname }}</text>
  28. </view>
  29. </view>
  30. <view :id="item.letter" v-for="item in cityData" :key="item.letter+1">
  31. <!-- ABCD -->
  32. <text class="letter">{{ item.letter }}</text>
  33. <!-- 城市 -->
  34. <view class="city-item">
  35. <view :class=" city== cityname?'city-item-viewa':'city-item-view' "
  36. v-for="(cityname, i) in item.list" :key="cityname + 5 * i" @click="onSelect(cityname)">
  37. <text :class=" city== cityname?'city-item-texta':'city-item-text' ">{{ cityname }}</text>
  38. </view>
  39. </view>
  40. </view>
  41. </scroll-view>
  42. <!-- 右边锚点 -->
  43. <view class="anchor" @touchstart="start" @touchmove="move" @touchend="end" v-if="sliding">
  44. <view>
  45. <view class="anchor-item" @click="scrollIntoId='current' "><text class="anchor-text">#</text></view>
  46. <view class="anchor-item" v-for="(item,index) in anchorArr" :key="item + 2" @click="scrollIntoId=item">
  47. <text class="anchor-text">{{item}}</text>
  48. </view>
  49. </view>
  50. </view>
  51. <view class="anchor" v-else>
  52. <view>
  53. <view class="anchor-item" @click="scrollIntoId='current' "><text class="anchor-text">#</text></view>
  54. <view class="anchor-item" v-for="(item,index) in anchorArr" :key="item + 3" @click="scrollIntoId=item">
  55. <text class="anchor-text">{{item}}</text>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. // hotCitys 热门城市
  63. // value 当前选中城市
  64. // windowHeight scroll的高 也是滑块的高 记得带单位!!! px rpx upx都支持
  65. // sliding 是否开始滑动选择 默认开启 false true
  66. // @onSelect 点击切换城市事件 参数为城市名称
  67. import cityData from './cityData.js'
  68. let anchorArr = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X",
  69. "Y", "Z"
  70. ];
  71. export default {
  72. props: {
  73. hotCitys: {
  74. type: Array,
  75. default () {
  76. return []
  77. }
  78. },
  79. value: {
  80. type: String
  81. },
  82. windowHeight: {
  83. type: String
  84. },
  85. // 开启滑动选择
  86. sliding: {
  87. type: Boolean,
  88. default: true
  89. },
  90. },
  91. data() {
  92. return {
  93. cityData,
  94. scrollIntoId: '',
  95. city: this.value,
  96. anchorArr,
  97. // 滑动中选中的具体值
  98. slidingChoice: "",
  99. //开始滑动位置
  100. startY: "",
  101. // 滑动中当前选中下标
  102. downIndex: -1,
  103. }
  104. },
  105. computed: {},
  106. methods: {
  107. // 点击城市
  108. onSelect(city) {
  109. this.city = city;
  110. this.$emit('onSelect', city)
  111. },
  112. // 开始滑动
  113. start(e) {
  114. // console.log("开始滑动",e);
  115. // #ifdef MP
  116. let eY = e.changedTouches[0].clientY
  117. // #endif
  118. // #ifdef APP-PLUS
  119. let eY = e.changedTouches[0].screenY
  120. // #endif
  121. this.startY = eY;
  122. // 计算点击时候下标
  123. let index = eY / (uni.upx2px(eY) / eY);
  124. index = parseInt((index - 200) / 40) - 1;
  125. this.downIndex = index;
  126. },
  127. // 开始移动
  128. move(e) {
  129. // console.log("开始移动",e);
  130. // #ifdef MP
  131. let downY = e.changedTouches[0].clientY;
  132. // #endif
  133. // #ifdef APP-PLUS
  134. let downY = e.changedTouches[0].screenY;
  135. // #endif
  136. let gap = this.startY - downY;
  137. let index = parseInt(gap / (uni.upx2px(gap) / gap) / 40);
  138. // 选中下标
  139. let optforIndex = this.downIndex - index;
  140. if (optforIndex < -1) {
  141. optforIndex = -1;
  142. } else if (optforIndex > 21) {
  143. optforIndex = 21;
  144. }
  145. this.slidingChoice = this.anchorArr[optforIndex];
  146. this.scrollIntoId = this.anchorArr[optforIndex];
  147. },
  148. // 滑动结束
  149. end(e) {
  150. // console.log("结束滑动",e)
  151. this.slidingChoice = '';
  152. },
  153. BackPage() {
  154. this.$emit("closeLhSelectCityFun")
  155. }
  156. }
  157. }
  158. </script>
  159. <style scoped>
  160. ::v-deep.select-city-wrap {
  161. height: 100vh;
  162. overflow: hidden;
  163. z-index: 9999;
  164. }
  165. /* 滑块 */
  166. .scroll {
  167. /* background-color: yellow; */
  168. }
  169. .name {
  170. color: #333;
  171. font-size: 28rpx;
  172. margin: 30rpx 30rpx;
  173. }
  174. /* 城市 */
  175. .letter {
  176. width: 44rpx;
  177. height: 44rpx;
  178. color: #fff;
  179. border-radius: 22rpx;
  180. background-color: #2f9bfe;
  181. font-size: 28rpx;
  182. line-height: 44rpx;
  183. text-align: center;
  184. margin-bottom: 30rpx;
  185. margin-left: 30rpx;
  186. display: inline-block;
  187. }
  188. .city-item {
  189. display: flex;
  190. flex-wrap: wrap;
  191. flex-direction: row;
  192. margin-left: 20rpx;
  193. }
  194. .city-item-view {
  195. width: 180rpx;
  196. height: 55rpx;
  197. margin: 15rpx;
  198. border: 1rpx solid #dcdcdc;
  199. border-radius: 6rpx;
  200. display: flex;
  201. justify-content: center;
  202. align-items: center;
  203. background-color: #FFFFFF;
  204. /* background-color: #d5ebff;
  205. border-color: #2f9bfe; */
  206. }
  207. .city-item-viewa {
  208. width: 180rpx;
  209. height: 55rpx;
  210. margin: 15rpx;
  211. border: 1rpx solid #2f9bfe;
  212. border-radius: 6rpx;
  213. display: flex;
  214. justify-content: center;
  215. align-items: center;
  216. background-color: #d5ebff;
  217. }
  218. .city-item-text {
  219. color: #999;
  220. font-size: 28rpx;
  221. }
  222. .city-item-texta {
  223. font-size: 28rpx;
  224. color: #2f9bfe;
  225. }
  226. /* 右锚点 */
  227. .anchor {
  228. /* background-color: pink; */
  229. position: fixed;
  230. right: 20rpx;
  231. top: 200rpx;
  232. z-index: 10;
  233. flex-direction: row;
  234. }
  235. .anchor-item {
  236. align-items: center;
  237. flex-direction: row;
  238. height: 40rpx;
  239. text-align: center;
  240. }
  241. .anchor-text-position {
  242. height: 40rpx;
  243. width: 40rpx;
  244. text-align: center;
  245. line-height: 40rpx;
  246. font-size: 32rpx;
  247. color: #333;
  248. background-color: #E5E5E5;
  249. border-radius: 20rpx;
  250. }
  251. .anchor-text {
  252. font-size: 32rpx;
  253. line-height: 40rpx;
  254. padding: 0 15rpx;
  255. color: #2f9bfe;
  256. }
  257. .action {
  258. height: 60rpx;
  259. }
  260. .action-box {
  261. position: fixed;
  262. left: 20rpx;
  263. top: 0;
  264. width: 100vw;
  265. height: 60rpx;
  266. box-sizing: border-box;
  267. background-color: #fff;
  268. display: flex;
  269. justify-content: space-between;
  270. align-items: center;
  271. }
  272. .backText {}
  273. </style>