<template>
<div>
<div class="van-picker__columns">
<div class="van-picker-column">
<ul class="van-picker-column__wrapper" ref="sheng" @touchstart="TouchStart"
@touchmove="TouchMove($event, 'sheng', 'shengmove', 'shengY')"
@touchend="TouchEnd('sheng', 'shengmove', 'shengY', 'shenglist')">
<li class="van-picker-column__item van-picker-column__item--selected">
<div class="van-ellipsis">请选择省份</div>
</li>
<li class="van-picker-column__item van-picker-column__item--selected" v-for="item in shenglist"
:key="item">
<div class="van-ellipsis">{{ item }}</div>
</li>
</ul>
</div>
<div class="van-picker-column">
<ul class="van-picker-column__wrapper" ref="shi" @touchstart="TouchStart"
@touchmove="TouchMove($event, 'shi', 'shimove', 'shiY')"
@touchend="TouchEnd('shi', 'shimove', 'shiY', 'shilist')">
<li class="van-picker-column__item van-picker-column__item--selected">
<div class="van-ellipsis">请选择市区</div>
</li>
<li class="van-picker-column__item van-picker-column__item--selected" v-for="item in shilist"
:key="item">
<div class="van-ellipsis">{{ item }}</div>
</li>
</ul>
</div>
<div class="van-picker-column">
<ul class="van-picker-column__wrapper" ref="qu" @touchstart="TouchStart"
@touchmove="TouchMove($event, 'qu', 'qumove', 'quY')"
@touchend="TouchEnd('qu', 'qumove', 'quY', 'qulist')">
<li class="van-picker-column__item van-picker-column__item--selected">
<div class="van-ellipsis">请选择区域</div>
</li>
<li class="van-picker-column__item van-picker-column__item--selected" v-for="item in qulist"
:key="item">
<div class="van-ellipsis">{{ item }}</div>
</li>
</ul>
</div>
<div class="van-picker__mask"></div>
<div class="van-hairline-unset--top-bottom van-picker__frame"></div>
</div>
</div>
</template>
<script>
import axios from "axios"
export default {
data() {
return {
startY: 0,
fuzhi: 110,
shenglist: [],
shilist: [],
qulist: [],
shengY: 0,
shengmove: 0,
shimove: 0,
shiY: 0,
qumove: 0,
quY: 0,
}
},
methods: {
TouchStart(e) {
this.startY = e.changedTouches[0].pageY;
},
TouchMove(e, params, move, Y) {
this[move] = this[Y] + e.changedTouches[0].pageY - this.startY
this.$refs[params].style = `transform:translate3d(0px, ${this[move] + this.fuzhi}px, 0px)`;
},
TouchEnd(params, move, Y, list) {
this[Y] = Math.round(this[move] / 44) * 44 >= 0 ? 0 : Math.round(this[move] / 44) * 44
if (Math.round(this[Y] / 44) * 44 <= -(this[list].length + 2) * 44 + this.fuzhi - 22) {
this[Y] = -(this[list].length + 2) * 44 + this.fuzhi - 22
}
this.$refs[params].style = `transform:translate3d(0px, ${Math.round(this[Y] / 44) * 44 + this.fuzhi}px, 0px); transition: all 0.3s;`;
if (params === 'sheng') {
axios.get('http://hmajax.itheima.net/api/city', {
params: {
pname: this.shenglist[-Math.round(this.shengY / 44) - 1]
}
}).then(res => {
this.shilist = res.data.list
this.shimove = 0
this.shiY = 0
this.$refs.shi.style = `transform:translate3d(0px, 110px, 0px); transition: all 0.3s;`;
this.qumove = 0
this.quY = 0
this.$refs.qu.style = `transform:translate3d(0px, 110px, 0px); transition: all 0.3s;`;
})
}
if (params === 'shi') {
axios.get('http://hmajax.itheima.net/api/area', {
params: {
pname: this.shenglist[-Math.round(this.shengY / 44) - 1],
cname: this.shilist[-Math.round(this.shiY / 44) - 1],
}
}).then(res => {
this.qulist = res.data.list;
console.log(this.qulist);
this.qumove = 0
this.quY = 0
this.$refs.qu.style = `transform:translate3d(0px, 110px, 0px); transition: all 0.3s;`;
})
}
this.$emit('val',
[this.shenglist[-Math.round(this.shengY / 44) - 1]
, this.shilist[-Math.round(this.shiY / 44) - 1],
this.qulist[-Math.round(this.quY / 44) - 1]])
},
},
created() {
axios.get('http://hmajax.itheima.net/api/province').then(res => {
this.shenglist = res.data.list
})
},
}
</script>
<style scoped lang="scss">
$r: 37.5;
.van-picker__columns {
height: 264rem/$r;
position: relative;
display: -webkit-box;
display: -webkit-flex;
display: flex;
cursor: grab;
}
.van-picker-column {
-webkit-box-flex: 1;
-webkit-flex: 1;
flex: 1;
overflow: hidden;
font-size: 0.42667rem;
}
.van-picker-column__wrapper {
-webkit-transition-timing-function: cubic-bezier(.23, 1, .68, 1);
transition-timing-function: cubic-bezier(.23, 1, .68, 1);
transform: translateY(110px);
}
ol,
ul {
margin: 0;
padding: 0;
list-style: none;
}
.van-picker-column__item {
height: 44rem/$r;
display: -webkit-box;
display: -webkit-flex;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
justify-content: center;
padding: 0 0.10667rem;
color: #000;
}
.van-ellipsis {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.van-picker__mask {
background-size: 100% 110px;
position: absolute;
top: 0;
left: 0;
z-index: 1;
width: 100%;
height: 100%;
background-image: -webkit-linear-gradient(top, hsla(0, 0%, 100%, .9), hsla(0, 0%, 100%, .4)), -webkit-linear-gradient(bottom, hsla(0, 0%, 100%, .9), hsla(0, 0%, 100%, .4));
background-image: linear-gradient(180deg, hsla(0, 0%, 100%, .9), hsla(0, 0%, 100%, .4)), linear-gradient(0deg, hsla(0, 0%, 100%, .9), hsla(0, 0%, 100%, .4));
background-repeat: no-repeat;
background-position: top, bottom;
-webkit-transform: translateZ(0);
transform: translateZ(0);
pointer-events: none;
}
.van-picker__frame {
position: absolute;
top: 50%;
right: 0.42667rem;
left: 0.42667rem;
height: 44rem/$r;
z-index: 2;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
pointer-events: none;
}
[class*=van-hairline]::after {
position: absolute;
box-sizing: border-box;
content: ' ';
pointer-events: none;
top: -50%;
right: -50%;
bottom: -50%;
left: -50%;
border: 0 solid #ebedf0;
-webkit-transform: scale(.5);
transform: scale(.5);
border-width: 0.02667rem 0;
}
</style>
用法
<div role="button" tabindex="0" class="van-cell van-cell--clickable van-field">
<div class="van-cell__title van-field__label"><span>地区</span></div>
<div class="van-cell__value van-field__value">
<div class="van-field__body"><input type="text" readonly="readonly"
placeholder="选择省 / 市 / 区" class="van-field__control" v-model="from.location"
@focus="ssx">
<div class="van-field__right-icon"><i class="van-icon van-icon-arrow"></i></div>
</div>
</div>
</div>
js:
data(){
return {
from: {
username: '',
userphone: '',
location: '',
useraddress: ''
},
value: '',
flgs: false,
checked: false,
add:[]
}}
methods:{
ssx() {
this.$refs.uio.style.zIndex = 2000
this.$refs.iop.style.bottom = 0
this.$refs.iop.style.zIndex = 2100
},
lopss(val) {
console.log(val);
this.value = val
},
que() {
this.$refs.uio.style.zIndex = -2
this.$refs.iop.style.bottom = -300
this.$refs.iop.style.zIndex = -3
this.from.location = this.value.join('/')
}
}