Element分析(組件篇)——Switch

switch組件是用來表示對立關系的切換的。

生命周期

mounted

mounted() {
  if (this.width === 0) {  // 如果沒有傳 width
    this.coreWidth = this.hasText ? 58 : 46;  // 有文字的時候是 58px, 沒有文字則是 46px
  }

  this.handleButtonTransform();  // 移動按鈕
  if (this.onColor || this.offColor) {  // 如果設置了顏色
    this.setBackgroundColor();
  }
}

handleButtonTransform

這是用來處理按鈕的位移的方法。

methods: {
  handleButtonTransform() {
    // 根據開關移動圓點
    this.buttonStyle.transform = this.value ? `translate(${ this.coreWidth - 20 }px, 2px)` : 'translate(2px, 2px)';
  },
}

setBackgroundColor

這是用來改變開關顏色的。

methods: {
  setBackgroundColor() {
    // 根據開關設置顏色
    let newColor = this.value ? this.onColor : this.offColor;

    // 改變邊框和背景色
    this.$refs.core.style.borderColor = newColor;
    this.$refs.core.style.backgroundColor = newColor;
  }
}

label

最外面是label,上面會根據是否禁用以及是否有文字來設置不同的類名。

<label
  class="el-switch"
  :class="{
    'is-disabled': disabled,
    'el-switch--wide': hasText
  }">
</label>

hasText

只要傳入了on-text或者off-text就表明有文字。

computed: {
  hasText() {
    return this.onText || this.offText;
  },
}

mask

暫時沒發現遮罩的作用。

<div class="el-switch__mask" v-show="disabled"></div>

模擬原生input

<input
  class="el-switch__input"
  type="checkbox"
  @change="handleChange"
  v-model="_value"
  :name="name"
  :disabled="disabled">

handleChange

處理change事件。

methods: {
  handleChange(event) {
    this.$emit('change', event.currentTarget.checked);  // 派發 change 事件
  },
}

_value

用來保存開關的情況,通過input事件來使用讓父級改變value,從而進而改變get獲得的值。

computed: {
  _value: {
    get() {
      return this.value;
    },
    set(val) {
      this.$emit('input', val);
    }
  }
}

核心部分

這一部分是用戶真正接觸的開關部分。

<span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }">
  <span class="el-switch__button" :style="buttonStyle"></span>
</span>

其中寬度用了coreWidth,而buttonStyle中目前只設置了transform

ON 標簽

這一部分是用來實現ON標簽的。

<transition name="label-fade">
  <div
    class="el-switch__label el-switch__label--left"
    v-show="value"
    :style="{ 'width': coreWidth + 'px' }">
    <i :class="[onIconClass]" v-if="onIconClass"></i>
    <span v-if="!onIconClass && onText">{{ onText }}</span>
  </div>
</transition>

label-fade

只是簡單的改變了透明度。

& .label-fade-enter,
& .label-fade-leave-active {
  opacity: 0;
}

onIconClass

這是用來設置on的時候顯示的圖標的,如果設置了這個就不會再顯示文字。

props: {
  onIconClass: {
    type: String,
    default: ''
  },
}

OFF 標簽

這與ON類似,不再進行細致分析。

<transition name="label-fade">
  <div
    class="el-switch__label el-switch__label--right"
    v-show="!value"
    :style="{ 'width': coreWidth + 'px' }">
    <i :class="[offIconClass]" v-if="offIconClass"></i>
    <span v-if="!offIconClass && offText">{{ offText }}</span>
  </div>
</transition>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • HTML標簽解釋大全 一、HTML標記 標簽:!DOCTYPE 說明:指定了 HTML 文檔遵循的文檔類型定義(D...
    米塔塔閱讀 3,331評論 1 41
  • [TOC] ##Assoc 顯示或修改文件擴展名關聯 Assoc [.Ext[=[Filetype]]] .Ex...
    btijjj閱讀 382評論 0 1
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,255評論 4 61
  • 從馬文事件開始,藝人的出軌接二連三成為大眾的飯后談資。 最終,馬司令一句:且行且珍惜;陳赫前女友一不開心環游世界;...
    喵姐看看閱讀 922評論 2 3