原生switch选择器(vue组件)

选择器
1.图片如下,有从左往右运动效果哦。


在这里插入图片描述
2.部分细节讲解:
2.1 大div包小 绝对定位
父元素圆角:
border-top-left-radius: 15px;
border-top-right-radius: 15px;
border-bottom-left-radius: 15px;
border-bottom-right-radius: 15px;
子元素圆角:border-radius:50%
2.2运动:
animation: .5s back; 有从左往右的运动效果
animation-fill-mode : forwards 让子元素不会自动回来
2.3防止首次进入页面子元素运动
this.$refs.son.style.animation = ‘none’
建议直接放到项目中试用
代码如下

<template>
  <div class='fat' @click="taggle" @click.once="rmstyle" :class="{fatopen:isClo,fatclose:!isClo}">
    <div ref="son" class="son" :class="{open:isClo,sonclose:!isClo}"></div>
  </div>
</template>
<script>
export default{
    data () {
        return {
          isClo: false
        }
    },
    methods: {
      taggle() {
        this.isClo = !this.isClo;
        this.$emit('isshow',this.isClo);
      },
      rmstyle() {
        this.$refs.son.style.animation = ''
      }
    },
    props:['isshow'],
    mounted(){
      this.$refs.son.style.animation = 'none'
    }
}
</script>
<style lang='scss' scoped>
.fat{
  position: relative;
  display: inline-block;
  width: 60px;
  height: 30px;
  background: #2e3333;
  border-top-left-radius: 15px;
  border-top-right-radius: 15px;
  border-bottom-left-radius: 15px;
  border-bottom-right-radius: 15px;
  .son{
    position: absolute;
    width: 30px;
    height: 30px;
    background-color: #f7f4f5;
    border-radius: 50%;
  }
}
.fatopen{
  background: #19d419;
}
.fatclose{
  background: #2e333385;
}
.open{
  animation: .5s move;
  animation-fill-mode : forwards 
}
.sonclose{
  animation: .5s back;
  animation-fill-mode : forwards
}
@keyframes move{
  0%{
    left: 2px;
    top: 0px;
  }
  100%{
    left: 28px;
    top: 0px;
  }
}
@keyframes back{
  0%{
    left: 28px;
    top: 0px;
  }
  100%{
    left: 2px;
    top: 0px;
  }
}  
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值