VUE实现Studio管理后台(九):开关(Switch)控件,输入框input系列

接下来几篇作文,会介绍用到的输入框系列,今天会介绍组普通的调用方式,因为RXEditor要求复杂的输入功能,后面的例子会用VUE的component动态调用,就没有今天的这么直观了,控件的实现原理都一样,只是调用方式的区别,今天的例子的调用代码为了写作文特殊制作的,写完作文还要恢复回去。
开关控件(Switch)的实现效果:

在这里插入图片描述

给组件取个好听的名字,叫RxSwitch吧。调用代码:

<RxSwitch 
  :onValue = "'on-value'" 
  :offValue = "'off-value'" 
  v-model = "inputValue"
\>
</RxSwitch\>

组件代码:

<template>
  <div class="rx-switch" 
    :class="onValue === inputValue? 'on' : ''"
    @click="click">
  </div>
</template>

<script>
export default {
  name: 'RxSwitch',
  props:{
    value:{ default:'' }, 
    onValue:{ default:'on' },
    offValue:{ default:'off' },
  },
  computed:{
    inputValue: {
      get:function() {
        return this.value;
      },
      set:function(val) {
        this.$emit('input', val);
      },
    },
  },
  methods: {
    click(){
      this.inputValue = this.inputValue === this.onValue 
                        ? this.offValue 
                        : this.onValue
    },
  },
}
</script>

<style>
.rx-switch{
  position: relative;
  width: 26px;
  height: 14px;
  background: #424242;
  margin-top:4px;
  border-radius:6px;
  cursor: pointer;
}

.rx-switch::after{
  content: '';
  position: absolute;
  top:-1px;
  left:-1px;
  height: 16px;
  width: 16px;
  background: #e0e0e0;
  border-radius: 50%;
  box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 0.4); 
}

.rx-switch.on{
  background: #75b325;
}

.rx-switch.on::after{
  content: '';
  position: absolute;
  top:-1px;
  left:auto;
  right: -1px;
  height: 16px;
  width: 16px;
  background: #e0e0e0;
  border-radius: 50%;
  box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 0.4); 
}
</style>

原理:鼠标点击时,切换v-model(inputValue)的值,模板根据inputValue的值确定是否显示on状态。

通过css伪类::after绘制开关上的触控点。具体可以参看CSS代码。

感谢耐心阅读,详细代码,请参考Github:https://github.com/vularsoft/studio-ui
若有有问题,请留言交流。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值