修改input type=radio和selecet默认样式,并实现单选点击文字选中选项

想要实现点击文字就选中选项,经过查找资料,学习到把input放进label中可以实现这一功能。

通常在使用input单选框达到单选功能,是通过控制checked的值来实现。而使用vue实现radio的单选,是将v-model与value配合使用,通过设置固定value,切换v-model中的值实现单选效果。话不多说,直接上代码更直观:

image.png

<div class="divFlex">
   <span>性别:</span>
   <p>
     <label class="label-input"><input type="radio" class="radio" v-model="i.brand" value="男"> 男</label>
     <label class="label-input"><input type="radio" class="radio" v-model="i.brand" value="女"> 女</label>
    </p>
 </div>

样式:

/* 先清除默认样式 */
 /* 设置input placeholder样式 */
    input::-webkit-input-placeholder, select::-webkit-input-placeholder {
      /* WebKit browsers */
      color: #ccc;
    }
    input:-moz-placeholder, select:-moz-placeholder{
      /* Mozilla Firefox 4 to 18 */
      color: #ccc;
    }
    input::-moz-placeholder, select::-moz-placeholder {
      /* Mozilla Firefox 19+ */
      color: #ccc;
    }
    input::-ms-input-placeholder, select::-ms-input-placeholder {
      /* Internet Explorer 10+ */
      color: #ccc;
    }
    /* input、select 默认样式清除 */
    input, select{
      outline: none;
      appearance: none;
      border-radius: 0;
      -webkit-appearance: none;/** 去除系统默认样式 */
      -webkit-tap-highlight-color: rgba(0, 0, 0, 0)/** 点击高亮 */
    }
    
/* 自定义radio样式 --start  */
    .divFlex p .label-input{
      position: relative;
      display: inline-block;
      width: 80px;
      height: 20px;
      line-height: 20px;
      padding-left: 15px;
    }
    .divFlex p .label-input input[type="radio"]::before{
      display: inline-block;
      position: absolute;
      top: 4px;
      left: 0;
      content: '';
      width: 10px;
      height: 10px;
      border-radius: 50%;
      border: 1px solid #FF4D4F;
      margin-right: 15px;
    }
    .divFlex p .label-input input[type="radio"]:checked::before{
      background-clip: content-box;
      background-color: #FF4D4F;
      width: 6px;
      height: 6px;
      padding: 2px;
    }
    .divFlex p .label-input input[type="radio"]{
      width: 0;
      height: 0;
    }
    /* 自定义radio样式 --end  */
    
    
/* 自定义select三角样式  -- start*/
    .divFlex p .select_triangle{
      width: 0;
      height: 0;
      border-width: 6px 6px 0;
      border-style: solid;
      border-color: #eee transparent transparent;
      position: absolute;
      right: 10px;
      top: 12px;
    }
    .divFlex p .select_triangle:hover{
      transform: rotate(90deg);
    }
    /* 自定义select三角样式  -- end */

每天学一点小技巧,生活将变的更美好!

评论

暂无评论