index.vue 6.8 KB

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