<template>
<div class="choice">
<ul ref='ul' v-bind:style="{width:ulWidth+'px'}">
<li
v-for="(item,index) in list"
:key="item.id"
:class="active===index?'active':''"
@click="select(index)"
>{{item.name}}</li>
</ul>
</div>
</template>
<script>
export default {
props: ["list"],
data() {
return {
active: 0,
ulWidth:0
};
},
mounted(){
this.computeUlWidth()
},
created() {
},
methods: {
computeUlWidth(){
const lis=this.$refs['ul'].children
for(let i=0;i<lis.length;i++){
const element=lis[i]
console.dir(element)
console.log('hh'+element.clientWidth)
this.ulWidth+=element.clientWidth
this.ulWidth+=30
}
this.ulWidth+=20
},
select(index) {
this.active = index;
}
},
components: {}
};
</script>
<style lang='scss' scoped>
.choice {
overflow-x: scroll;
ul {
height: 30px;
margin: 5px 10px;
overflow-x: auto;
overflow-y: hidden;
display: flex;
li {
display: flex;
line-height: 30px;
padding: 0 20px;
// float: left;
border-radius: 20px;
&.active {
// height: ;
color: #ed9e37;
background-color: #f4f4f4;
}
}
}
}
</style>
我动态计算ul的宽度时,每个li的clientWidth都是一样的。这就有问题。但是不知道为什么都是一样的。
考虑一下
nextTick
的问题:https://segmentfault.com/a/11...