小程序switch 组件自定义

实现效果
image

  1. 使用小程序原生组件,对switch进行样式修改
    缺点:宽高增加的话,增的那部分点击切换时不太灵敏

方法一:直接修改css样式

/*swtich整体大小*/
.switch-item{
    width:105rpx !important;
    height: 40rpx !important;
}
/*白色样式(switch关闭状态的样式)*/
.switch-item::before{
    width:105rpx !important;
    height: 40rpx !important;
    background-color: #EBEDF5 !important;
}
/*绿色样式(switch打开状态的样式)*/
.switch-item::after{
    width: 38rpx !important; 
    height: 38rpx !important;
    box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3);
    left: 0 !important;
}

方法二: 使用zoom缩放

.switch-item{
    zoom: 1.1; // 数字越大  放大倍数越大
}
  1. 自定义switch
 
     
        
     
    背诵模式
.switch-box {
    position: relative;
}
.switch-box text {
    position: absolute;
    color: #B7B8BB;
    font-size: 16rpx;
    line-height: 40rpx;
    right: 10rpx;
    top: 0;
}

.switch {
    width: 100%;
    height: 40rpx;
    background-color: #1180FD;
    border-radius: 9999rpx;
    position: relative;
}
.switch::before {
    display: block;
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 9999rpx;
    background: #EBEDF5;
    transition: all 0.35s cubic-bezier(0.45, 1, 0.4, 1);
}


.switch-checked::before {
    transform: scale(0);
}

.switch view {
    position: absolute;
    top: 0;
    left: 0;
    width: 40rpx;
    height: 40rpx;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.4);
    transition: all 0.35s cubic-bezier(0.45, 1, 0.4, 1);
}

.switch-checked view {
    left: 100%;
    transform: translateX(-100%);
}

你可能感兴趣的