在项目开发中,我们经常会碰到手机端按钮开关切换的功能,而在Vue中想实现这样的功能也应该有很多种,用ui组件,那么用vue原生如何实现呢
效果图如下:
css部分:
.switch,
.switchActive{
width: 70px;
height: 30px;
background-color: red;
border-radius: 18px;
margin: 0 auto;
}
.switchActive{
background-color: green;
}
.switchItem,
.switchItemActive{
width: 30px;
height: 30px;
background-color: #fff;
border-radius: 50%;
}
.switchItemActive{
transform: translateX(41px);
transition: 2s;
}
HTML部分:
<div id="app">
<div :class="index==0?'switch':'switchActive'" @click="index=index==0?1:0">
<div :class="index==0?'switchItem':'switchItemActive'">
</div>
</div>
</div>
js部分:
const vm=new Vue({
el:'#app',
data:{
index:0
}
})