vue 在style标签中使用变量之 input(checkbox)类型

步骤1:定义变量

<script>
export default {
  data() {
    return {
     backgroundColor: app.theme.getStyle().base.color, // 整个背景色也可以作为全局主题颜色(获取到的全局变量) this.global.getRandColor
     bgColor: "pink", //背景色
    };
  },
};
</script>

步骤2:在HTML中设置CSS使用的变量

<template>
    <input
      type="checkbox"
      class="inp"
      :style="{ '--backgroundColor': backgroundColor }"
    />主题色(随主题颜色变动) <br /><br />
    <input
      type="checkbox"
      class="inp"
      :style="{ '--backgroundColor': bgColor }"
    />自己可设置的颜色
</template>

步骤3:在style标签中使用变量

<style scoped>
.inp {
  width: 20px;
  height: 20px;
  /* 去除系统默认appearance的样式,常用于IOS下移除原生样式 */
  -webkit-appearance: none;
  border: 1px solid blue;
}

.inp:checked {
  /* 使用var()获取变量,参数就是变量名 */
  background-color: var(--backgroundColor);
}
</style>

效果展示

原图效果:

点击后效果:

 解读:

类型一

 错误一:input(checkbox)型中想改变选中状态的颜色 如果只是这样写的话是不好使的 

<style scoped>
.inp:checked {
  /* 使用var()获取变量,参数就是变量名 */
  background-color: var(--backgroundColor);
}
</style>

right:

<style scoped>
.inp {
  width: 20px;
  height: 20px;
  /* 去除系统默认appearance的样式,常用于IOS下移除原生样式 */
  -webkit-appearance: none;
  border: 1px solid blue;
}

.inp:checked {
  /* 使用var()获取变量,参数就是变量名 */
  background-color: var(--backgroundColor);
}
</style>

类型二

改变input(checkbox)类型默认方形为圆形或者其他图形

<style scoped>
.inp:checked {
  background: var(--backgroundColor);
}
.inp {
  width: 25px;
  height: 25px;
  background-color: #ffffff;
  border: solid 1px #dddddd;
  -webkit-border-radius: 50%;//外观的形状由我来决定哦!前缀为“-webkit-”的属性,能够在以 
  webkit为内核的浏览器中正常使用,例如“safari”和“chrome”浏览器。
  border-radius: 50%;//外观的形状由我来决定哦!
  font-size: 0.8rem;
  margin: 0;
  padding: 0;
  position: relative;
  display: inline-block;
  vertical-align: top;
  cursor: default;
  -webkit-appearance: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-transition: background-color ease 0.1s;
  transition: background-color ease 0.1s;
}
.inp:checked::after {
  content: "";
  top: 5px;
  left: 5px;
  position: absolute;
  background: transparent;
  border: #fff solid 2px;
  border-top: none;
  border-right: none;
  height: 6px;
  width: 10px;
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
}
</style>

效果展示

原图效果:

点击后效果:

 外观的改变样式中有注释。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值