uniapp开发微信小程序picker-view 组件修改选中框的样式

在项目中,很多人会遇到选择时间、地点、类别等需求,需要自定义选择器,那么这个选择框避免不要要改样式

  1. 要在picker-view上加 
    indicator-class="picker-box"
  2. 修改的样式为
     /deep/ .picker-box {
        width: 100%;
        height: 99rpx;
        background-color: rgba(26, 176, 104, 0.1);
        z-index: 1;
      }
      //给选中的添加border-radius
      /deep/ .view-column.second .picker-box {
        border-radius:0 20rpx 20rpx 0;
      }
      /deep/ .view-column.first .picker-box {
        border-radius: 20rpx 0 0 20rpx;
      }
      // 修改原有的上下边框颜色
      /deep/ .picker-box::after {
        border-bottom: 0 solid rgba(26, 176, 104, 0.1);
      }
      /deep/ .picker-box::before {
        border-top: 0 solid rgba(26, 176, 104, 0.1);
      }

    完整代码如下

    <template>
      <u-popup :show="show" @close="close" :round="20">
        <view class="change-date-modal">
          <view class="change-date-modal-header">
            <view class="change-date-modal-header-title">选择日期</view>
            <view class="change-date-modal-header-icon" @click="close">
              <uni-icons color="#D9D4D4" type="closeempty" size="20"></uni-icons>
            </view>
          </view>
          <picker-view indicator-class="picker-box" class="change-date-modal-picker" :value="dateValue"
                       @change="change" >
            <picker-view-column class="view-column first">
              <view class="change-date-modal-picker-item" v-for="(item,index) in yearArr"
                    :key="index" :class="{
                      'change-date-modal-picker-item-checked':index===dateValue[0]
                    }" >{{ item }}年
              </view>
            </picker-view-column>
            <picker-view-column class="view-column second">
              <view class="change-date-modal-picker-item" v-for="(item,index) in monthArr"
                    :class="{
                      'change-date-modal-picker-item-checked':index===dateValue[1]
                    }"
                    :key="index">{{ item }}月
              </view>
            </picker-view-column>
          </picker-view>
          <view class="change-date-modal-button" @click="sure">确定</view>
        </view>
      </u-popup>
    </template>
    
    <script>
    export default {
      name: "change-date-modal",
      props: {
        defaultDate: {
          type: Array,
          default: () => []
        }
      },
      data() {
        return {
          yearArr: [],
          monthArr: [],
          dateValue: [0, 0],
          show: false
        }
      },
      created() {
        const date = new Date();
        const yearList = [], monthList = [];
        for (let i = 2020; i <= date.getFullYear(); i++) {
          yearList.push(i);
        }
        for (let i = 1; i <= 12; i++) {
          monthList.push(i);
        }
        this.yearArr = yearList;
        this.monthArr = monthList;
      },
      methods: {
        close() {
          this.show = false;
        },
        change(e) {
          this.dateValue = e.detail.value;
        },
        open() {
          this.dateValue = [this.yearArr.indexOf(this.defaultDate[0]), this.monthArr.indexOf(this.defaultDate[1])];
          this.show = true;
        },
        sure() {
          const list = [this.yearArr[this.dateValue[0]],this.monthArr[this.dateValue[1]]]
          this.$emit("sure",list)
          this.show=false
        }
      }
    }
    </script>
    
    <style scoped lang="scss">
    @import "@/css/variable.scss";
    
    .change-date-modal {
      width: 100vw;
    
      &-header {
        @include flex-center;
        width: 100%;
        border-radius: 20rpx 20rpx 0 0;
        height: 88rpx;
        background: #F8F8F8;
    
        &-title {
          font-size: 28rpx;
          font-weight: 500;
          color: rgba(56, 56, 56, 1);
        }
    
        &-icon {
          position: absolute;
          right: 34rpx;
        }
      }
    
      &-picker {
        width: 100%;
        height: 600rpx;
        box-sizing: border-box;
        padding: 0 40rpx;
        &-item {
          font-size: 32rpx;
          font-weight: 400;
          color: rgba(128, 128, 128, 1);
          text-align: center;
          line-height: 99rpx;
        }
        &-item-checked{
          color: rgba(26, 176, 104, 1);
        }
      }
    
      /deep/ .picker-box {
        width: 100%;
        height: 99rpx;
        background-color: rgba(26, 176, 104, 0.1);
        z-index: 1;
      }
      //给选中的添加border-radius
      /deep/ .view-column.second .picker-box {
        border-radius:0 20rpx 20rpx 0;
      }
      /deep/ .view-column.first .picker-box {
        border-radius: 20rpx 0 0 20rpx;
      }
      // 修改原有的上下边框颜色
      /deep/ .picker-box::after {
        border-bottom: 0 solid rgba(26, 176, 104, 0.1);
      }
      /deep/ .picker-box::before {
        border-top: 0 solid rgba(26, 176, 104, 0.1);
      }
    
      &-button {
        width: 590rpx;
        height: 88rpx;
        opacity: 1;
        border-radius: 100rpx;
        background: rgba(26, 176, 104, 1);
        font-size: 32rpx;
        font-weight: 400;
        line-height: 88rpx;
        color: rgba(255, 255, 255, 1);
        text-align: center;
        margin: 40rpx auto;
      }
    }
    
    
    </style>
    
    
    

    效果

觉得不错的话麻烦点点赞,关注一下

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值