index.nvue 6.1 KB

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