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
若有有問題,請留言交流。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM