index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <view class="citylist">
  3. <scroll-view :style="{'height':windowH}" scroll-y="true" :scroll-top="scrollTop" show-scrollbar="false"
  4. @scroll="scroll">
  5. <view>
  6. <view class="city-list-container">
  7. <!-- 定位城市 -->
  8. <view class="city-list-content" id="location_city">
  9. <view class="city-title">定位城市</view>
  10. <view class="city-list city-list-inline" @tap="selectedCity(locationCity)">
  11. <view class="location-city">{{locationCity}}</view>
  12. </view>
  13. </view>
  14. <!-- 热门城市 -->
  15. <view id="hotcity" class="city-list-content">
  16. <view class="city-title">
  17. {{hotcity.title}}
  18. </view>
  19. <view class="city-list city-list-inline">
  20. <view class="city-item" v-for="(item,index) in hotcity.lists" :key="`city${index}`"
  21. @tap="selectedCity(item)">
  22. {{item}}
  23. </view>
  24. </view>
  25. </view>
  26. <!-- 城市列表 -->
  27. <view id="citytitle" class="city-list-content">
  28. <view class="city_title_wrap" v-for="(city,index) in citylist" :key="`citylist${index}`">
  29. <view class="city-title city-title-letter">
  30. {{city.title}}
  31. </view>
  32. <view class="city-list city-list-block">
  33. <view class="city-item" v-for="(item,index) in city.lists" :key="`item${index}`"
  34. @tap="selectedCity(item)">
  35. {{item}}
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </scroll-view>
  43. <!-- 固定顶部 -->
  44. <view class="fixtitle" :style="{transform:fixedStyle}">
  45. <view class="city-title">{{fixedTitle}}</view>
  46. </view>
  47. <!-- 侧边栏导航 -->
  48. <view class="navrightcity">
  49. <view class="nav-item nav-letter" @tap="scroll_to_city(0)">定</view>
  50. <view class="nav-item nav-letter" @tap="scroll_to_city(1)">热</view>
  51. <view v-for="(item,index) in citylist" :key="`nav${index}`" class="nav-item nav-letter"
  52. @click="scroll_to_city(index+2)">
  53. {{item.title}}
  54. </view>
  55. </view>
  56. </view>
  57. </template>
  58. <script>
  59. import CL from './citylist.json'
  60. export default {
  61. props: {
  62. getCity: {
  63. type: Function,
  64. default: function() {}
  65. }
  66. },
  67. computed: {
  68. fixedTitle() {
  69. if (this.scrollY < 0) {
  70. return ''
  71. }
  72. return this.title && this.title[this.currentIndex]
  73. }
  74. },
  75. watch: {
  76. scrollY(newY) {
  77. const {
  78. tops
  79. } = this
  80. const index = tops.findIndex((top, index) => {
  81. this.diff = tops[index + 1] - newY
  82. return newY >= top && newY < tops[index + 1]
  83. })
  84. this.currentIndex = index;
  85. },
  86. diff(newVal) {
  87. let fixedTop = (newVal > 0 && newVal < 30) ? newVal - 30 : 0
  88. if (this.fixedTop === fixedTop) {
  89. this.fixedStyle = `translate3d(0,0,0)`
  90. }
  91. this.fixedTop = fixedTop
  92. this.fixedStyle = `translate3d(0,${this.fixedTop}px,0)`
  93. }
  94. },
  95. data() {
  96. return {
  97. scrollY: -1, //滚动记录
  98. tops: [], //每一个.city-title 距离顶部的距离
  99. diff: -1, //
  100. citylist: {},
  101. hotcity: {},
  102. currentIndex: 0,
  103. title: [],
  104. windowH: "",
  105. scrollTop: 0,
  106. fixedStyle: "translate3d(0,0,0)",
  107. fixedTop: 0,
  108. locationCity: "定位中...."
  109. }
  110. },
  111. methods: {
  112. // 初始化数据列表
  113. initCityList() {
  114. let title = [];
  115. this.hotcity = CL.hotcity;
  116. this.citylist = CL.city
  117. title.push("定位城市");
  118. title.push(this.hotcity.title);
  119. for (let i in this.citylist) {
  120. title.push(this.citylist[i].title)
  121. }
  122. this.title = title;
  123. let sysInfo = uni.getSystemInfoSync();
  124. this.windowH = sysInfo.windowHeight + "px"
  125. },
  126. initTop() {
  127. // 1. 初始化tops
  128. const query = uni.createSelectorQuery();
  129. query.select('#location_city').boundingClientRect()
  130. .select('#hotcity').boundingClientRect()
  131. .selectAll('.city_title_wrap').boundingClientRect()
  132. .exec((list) => {
  133. let tops = []
  134. let top = 0
  135. tops.push(top);
  136. if (list[0]) {
  137. top += list[0].height;
  138. tops.push(top)
  139. }
  140. if (list[1]) {
  141. top += list[1].height;
  142. tops.push(top)
  143. }
  144. if (list[2].length !== 0) {
  145. for (let i in list[2]) {
  146. top += list[2][i].height
  147. tops.push(top);
  148. }
  149. }
  150. this.tops = tops;
  151. })
  152. },
  153. // 获取城市
  154. // selectedCity({city}){
  155. selectedCity(city) {
  156. console.log(city);
  157. let That = this;
  158. uni.chooseLocation({
  159. keyword: city,
  160. success(res) {
  161. console.log(res,'choose location');
  162. uni.setStorage({
  163. key: "location",
  164. data: [res.longitude, res.latitude]
  165. })
  166. console.log(res.address);
  167. // That.locationCity = res && res.address;
  168. // That.locationName = res && res.name;
  169. That.confrim({
  170. city: That.locationCity,
  171. name: That.locationName
  172. });
  173. },
  174. fail() {
  175. console.log('--------------------- fail')
  176. // That.locationCity = "定位失败,请点击重试";
  177. // That.locationName = "";
  178. }
  179. });
  180. },
  181. // 定位操作
  182. location() {
  183. let That = this;
  184. uni.getLocation({
  185. type: 'gcj02',
  186. geocode: true,
  187. success(res) {
  188. console.log(res);
  189. uni.setStorage({
  190. key: "location",
  191. data: [res.longitude, res.latitude]
  192. })
  193. console.log(res.address);
  194. That.locationCity = res && res.address && res.address.city;
  195. That.locationName = res && res.address && res.address.poiName;
  196. // That.selectedCity({
  197. // city: That.locationCity,
  198. // name: That.locationName
  199. // });
  200. },
  201. fail() {
  202. That.locationCity = "定位失败,请点击重试";
  203. That.locationName = "";
  204. }
  205. })
  206. },
  207. // 滚动条Y距离
  208. scroll(e) {
  209. this.scrollY = e.detail && e.detail.scrollTop;
  210. },
  211. // 滚动到指定位置
  212. scroll_to_city(index) {
  213. this.scrollTop = this.tops[index]
  214. this.scrollY = scrollY
  215. this.cityScroll.scrollTo(0, -scrollY, 300)
  216. },
  217. confrim(city){
  218. this.getCity && this.getCity({
  219. city
  220. });
  221. }
  222. },
  223. // 页面挂载后进行异步操作
  224. created() {
  225. this.initCityList();
  226. },
  227. mounted() {
  228. this.location();
  229. this.initTop();
  230. }
  231. }
  232. </script>
  233. <style lang="less">
  234. .citylist {
  235. width: 100%;
  236. height: 100%;
  237. overflow: hidden;
  238. position: relative;
  239. }
  240. .city-list-container {
  241. width: 100%;
  242. height: 100%;
  243. background-color: #ebebeb;
  244. font-size: 14px;
  245. color: #333;
  246. .city-list-content {
  247. margin-right: 25px;
  248. }
  249. .city-title {
  250. padding-left: 15px;
  251. line-height: 30px;
  252. }
  253. .city-list {
  254. padding-right: 30px;
  255. }
  256. .city-title-letter {
  257. padding-left: 25px;
  258. }
  259. .city-list-block {
  260. background-color: #f5f5f5;
  261. .city-item {
  262. height: 44px;
  263. line-height: 44px;
  264. margin-left: 15px;
  265. border-bottom: 1px solid #c8c7cc;
  266. &:last-child {
  267. border: 0;
  268. }
  269. }
  270. }
  271. .city-list-inline {
  272. background-color: #f5f5f5;
  273. padding-bottom: 8px;
  274. overflow: hidden;
  275. &::after {
  276. content: "";
  277. clear: both;
  278. }
  279. .location-city,
  280. .city-item {
  281. float: left;
  282. background: #fff;
  283. width: 29%;
  284. height: 33px;
  285. margin-top: 15px;
  286. margin-left: 4%;
  287. padding: 0 4px;
  288. border: 1px solid #e6e6e6;
  289. border-radius: 3px;
  290. line-height: 33px;
  291. text-align: center;
  292. box-sizing: border-box;
  293. white-space: nowrap;
  294. overflow: hidden;
  295. text-overflow: ellipsis;
  296. }
  297. .location-city {
  298. width: auto;
  299. min-width: 30%;
  300. padding: 0 20px;
  301. }
  302. }
  303. }
  304. .navrightcity {
  305. position: fixed;
  306. top: 50%;
  307. transform: translateY(-50%);
  308. right: 0;
  309. width: 35px;
  310. z-index: 10;
  311. text-align: center;
  312. font-size: 12px;
  313. .nav-item {
  314. height: 16px;
  315. height: 2.8vh;
  316. }
  317. .nav-letter {
  318. width: 15px;
  319. margin-left: 15px;
  320. }
  321. }
  322. .fixtitle {
  323. width: 100%;
  324. position: fixed;
  325. top: 0;
  326. left: 0;
  327. overflow: hidden;
  328. .city-title {
  329. padding-left: 15px;
  330. line-height: 30px;
  331. font-size: 14px;
  332. color: #333;
  333. background-color: #ebebeb;
  334. }
  335. }
  336. </style>