index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. uni.navigateBack({
  188. delta:1
  189. })
  190. }
  191. },
  192. // 页面挂载后进行异步操作
  193. created(){
  194. this.initCityList();
  195. },
  196. mounted(){
  197. this.location();
  198. this.initTop();
  199. }
  200. }
  201. </script>
  202. <style lang="less">
  203. .citylist{
  204. width: 100%;
  205. height: 100%;
  206. overflow: hidden;
  207. position: relative;
  208. }
  209. .city-list-container{
  210. width: 100%;
  211. height: 100%;
  212. background-color: #ebebeb;
  213. font-size: 14px;
  214. color: #333;
  215. .city-list-content{
  216. margin-right: 25px;
  217. }
  218. .city-title{
  219. padding-left: 15px;
  220. line-height: 30px;
  221. }
  222. .city-list{
  223. padding-right: 30px;
  224. }
  225. .city-title-letter {
  226. padding-left: 25px;
  227. }
  228. .city-list-block {
  229. background-color: #f5f5f5;
  230. .city-item {
  231. height: 44px;
  232. line-height: 44px;
  233. margin-left: 15px;
  234. border-bottom: 1px solid #c8c7cc;
  235. &:last-child{
  236. border: 0;
  237. }
  238. }
  239. }
  240. .city-list-inline {
  241. background-color: #f5f5f5;
  242. padding-bottom: 8px;
  243. overflow: hidden;
  244. &::after{
  245. content: "";
  246. clear: both;
  247. }
  248. .location-city,.city-item {
  249. float: left;
  250. background: #fff;
  251. width: 29%;
  252. height: 33px;
  253. margin-top: 15px;
  254. margin-left: 4%;
  255. padding: 0 4px;
  256. border: 1px solid #e6e6e6;
  257. border-radius: 3px;
  258. line-height: 33px;
  259. text-align: center;
  260. box-sizing: border-box;
  261. white-space: nowrap;
  262. overflow: hidden;
  263. text-overflow: ellipsis;
  264. }
  265. .location-city{
  266. width: auto;
  267. min-width: 30%;
  268. padding: 0 20px;
  269. }
  270. }
  271. }
  272. .navrightcity {
  273. position: fixed;
  274. top: 50%;
  275. transform: translateY(-50%);
  276. right: 0;
  277. width: 35px;
  278. z-index: 10;
  279. text-align: center;
  280. font-size: 12px;
  281. .nav-item {
  282. height: 16px;
  283. height: 2.8vh;
  284. }
  285. .nav-letter {
  286. width: 15px;
  287. margin-left: 15px;
  288. }
  289. }
  290. .fixtitle{
  291. width: 100%;
  292. position: fixed;
  293. top: 0;
  294. left: 0;
  295. overflow: hidden;
  296. .city-title{
  297. padding-left: 15px;
  298. line-height: 30px;
  299. font-size: 14px;
  300. color: #333;
  301. background-color: #ebebeb;
  302. }
  303. }
  304. .location-city-btn{
  305. color: #FFFFFF;
  306. margin: 15px 0 0 20px;
  307. height: 33px;
  308. background-color: #71cd9a;
  309. text-align: center;
  310. line-height: 33px;
  311. }
  312. </style>