index.vue 7.3 KB

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