先來一張效果圖:
city.vue
<template>
<div>
<city-header></city-header>
<city-search :cities="cities"></city-search>
<city-list :cities="cities" :hotCities="hotCities" :letter="letter"></city-list>
<city-alphabet :cities="cities" @change="letterChange"></city-alphabet>
</div>
</template>
<script>
import axios from 'axios'
import CityHeader from './components/Header'
import CitySearch from './components/Search'
import CityList from './components/List'
import CityAlphabet from './components/Alphabet'
export default {
name: 'City',
components: {CityAlphabet, CityList, CitySearch, CityHeader},
data () {
return {
cities: {},
hotCities: [],
letter: ''
}
},
methods: {
getCityInfo () {
axios.get('../../../static/mock/city.json')
.then(this.getCityInfoSucc)
},
letterChange (l) {
this.letter = l
},
getCityInfoSucc (res) {
res = res.data
if (res.ret && res.data) {
const data = res.data
this.cities = data.cities
this.hotCities = data.hotCities
}
}
},
mounted () {
this.getCityInfo()
}
}
</script>
<style scoped lang="stylus">
</style>
alphabet.vue
<template>
<ul class="list">
<li class="item"
v-for="(item, key) of cities"
:key="key"
:ref="key"
@click="letterClick"
@touchstart.prevent="touchStart"
@touchmove="touchMove"
@touchend="touchEnd"
>{{key}}</li>
</ul>
</template>
<script>
export default {
name: 'CityAlphabet',
props: {
cities: Object
},
data () {
return {
touchStatus: false,
startY: 0,
timer: null
}
},
updated () {
this.startY = this.$refs['A'][0].offsetTop
},
methods: {
letterClick (e) {
this.$emit('change', e.target.innerText)
},
touchStart () {
this.touchStatus = true
},
touchMove (e) {
if (this.touchStatus === true) {
if (this.timer) {
clearTimeout(this.timer)
}
this.timer = setTimeout(() => {
const startY = this.startY
const touchY = e.touches[0].clientY - 79
const index = Math.floor((touchY - startY) / 20)
if (index >= 0 && index < this.letters.length) {
this.$emit('change', this.letters[index])
}
}, 16)
}
},
touchEnd () {
this.touchStatus = false
}
},
computed: {
letters () {
const letters = []
for (let i in this.cities) {
letters.push(i)
}
return letters
}
}
}
</script>
<style scoped lang="stylus">
@import '~styles/varibles.styl'
.list
display flex
flex-direction column
justify-content center
position: absolute
top: 1.58rem
right: 0
bottom 0
width .4rem
.item
line-height: .4rem
text-align center
color: $bgColor
</style>
List.vue
<template>
<ul class="list">
<li class="item"
v-for="(item, key) of cities"
:key="key"
:ref="key"
@click="letterClick"
@touchstart.prevent="touchStart"
@touchmove="touchMove"
@touchend="touchEnd"
>{{key}}</li>
</ul>
</template>
<script>
export default {
name: 'CityAlphabet',
props: {
cities: Object
},
data () {
return {
touchStatus: false,
startY: 0,
timer: null
}
},
updated () {
this.startY = this.$refs['A'][0].offsetTop
},
methods: {
letterClick (e) {
this.$emit('change', e.target.innerText)
},
touchStart () {
this.touchStatus = true
},
touchMove (e) {
if (this.touchStatus === true) {
if (this.timer) {
clearTimeout(this.timer)
}
this.timer = setTimeout(() => {
const startY = this.startY
const touchY = e.touches[0].clientY - 79
const index = Math.floor((touchY - startY) / 20)
if (index >= 0 && index < this.letters.length) {
this.$emit('change', this.letters[index])
}
}, 16)
}
},
touchEnd () {
this.touchStatus = false
}
},
computed: {
letters () {
const letters = []
for (let i in this.cities) {
letters.push(i)
}
return letters
}
}
}
</script>
<style scoped lang="stylus">
@import '~styles/varibles.styl'
.list
display flex
flex-direction column
justify-content center
position: absolute
top: 1.58rem
right: 0
bottom 0
width .4rem
.item
line-height: .4rem
text-align center
color: $bgColor
</style>
search.vue
<template>
<div>
<div class="search">
<input v-model="keyword" class="search-input" type="text" placeholder="輸入城市名或拼音">
</div>
<div class="search-content" ref="search-content" v-show="keyword">
<ul>
<li class="search-item border-bottom" v-for="item of list" :key="item.id" @click="cityClick(item.name)">{{item.name}}</li>
<li class="search-item border-bottom" v-show="hasNoData">沒有找到匹配數據</li>
</ul>
</div>
</div>
</template>
<script>
import Bscroll from 'better-scroll'
export default {
name: 'CitySearch',
data () {
return {
keyword: '',
list: [],
timer: null
}
},
props: {
cities: Object
},
watch: {
keyword () {
if (this.timer) {
clearTimeout(this.timer)
}
if (!this.keyword) {
this.list = []
return
}
this.timer = setTimeout(() => {
const result = []
for (let i in this.cities) {
this.cities[i].forEach((value) => {
if (value.spell.indexOf(this.keyword) > -1 || value.name.indexOf(this.keyword) > -1) {
result.push(value)
}
})
}
this.list = result
}, 100)
}
},
methods: {
cityClick (city) {
this.$store.dispatch('changeCity', city)
this.$router.push('/')
}
},
mounted () {
this.scroll = new Bscroll(this.$refs['search-content'])
},
computed: {
hasNoData () {
return !this.list.length
}
}
}
</script>
<style scoped lang="stylus">
@import '~styles/varibles.styl'
.search
height .72rem
background $bgColor
padding 0 .1rem
.search-input
box-sizing border-box
height .62rem
line-height: .62rem
padding 0 .1rem
width 100%
text-align: center
border-radius .06rem
color: #666
.search-content
z-index 1
overflow: hidden
position absolute
top 1.58rem
left: 0
right: 0
bottom: 0
background #eee
.search-item
line-height: .62rem
padding-left: .2rem;
color: #666
background: #fff
</style>