Skip to content

Switch 开关

*UI备注

此模块为须严格遵守设计规范的模块

CSS样式

全部switch样式代码:

查看代码
css
/* HnuiIcon */
@import url('./icon.css');

/* 全局css变量 */

:root {
  /* #region vars-BrandColor */

  /* 品牌色 */
  --hnui-brand-color: #1f78ff;
  --hnui-brand-color1: #005ce5;
  --hnui-brand-color2: #4b83f7;
  --hnui-brand-color3: #3384ff;
  --hnui-brand-color4: #599bff;
  --hnui-brand-color5: #80b2ff;
  --hnui-brand-color6: #b2d1ff;
  --hnui-brand-color7: #cce0ff;
  --hnui-brand-color8: #d9e8ff;
  --hnui-brand-color9: #ebf3ff;
  --hnui-brand-color10: #f2f7ff;

  /* 状态颜色 */
  --hnui-brand-color-normal: var(--hnui-brand-color);
  --hnui-brand-color-hover: var(--hnui-brand-color3);
  --hnui-brand-color-active: var(--hnui-brand-color1);
  --hnui-brand-color-elected: var(--hnui-brand-color9);
  --hnui-brand-color-disabled: var(--hnui-brand-color7);

  --hnui-brand-color-disabled-border: var(--hnui-brand-color6);
  --hnui-brand-color-disabled-bg: var(--hnui-brand-color10);

  /* #endregion vars-BrandColor */

  /* #region vars-DomainColor */

  /* 功能色 */
  --hnui-success-color: #2ba471;
  --hnui-success-hover-color: #51bd90;
  --hnui-success-active-color: #2ba471;

  --hnui-warning-color: #faad14;
  --hnui-warning-hover-color: #ffc146;
  --hnui-warning-active-color: #db960b;

  --hnui-danger-color: #ff4d4d;
  --hnui-danger-hover-color: #ff7676;
  --hnui-danger-active-color: #d82a2a;

  --hnui-info-color: #8d97a6;
  --hnui-info-hover-color: #b2b7c0;
  --hnui-info-active-color: #707a89;

  /* #endregion vars-DomainColor */

  /* #region vars-NeutralColor */
  /* 中性色 */

  /* 标题文字 */
  --hnui-neutral-color-1: #2d2e33;

  /* 正文、重要信息文字、按钮文字、输入框正常状态文字等 */
  --hnui-neutral-color-2: #43454d;

  /* 内容文字、说明叙述文字 */
  --hnui-neutral-color-3: #5d606a;

  /* 提示性文字、辅助文本 */
  --hnui-neutral-color-4: #9298a0;

  /* 禁用文本 */
  --hnui-neutral-color-5: #b7bdc7;

  /* 描边格描边、输入文本描边、下拉选择描边等 */
  --hnui-neutral-color-6: #dfe2e7;

  /* 全局背景、禁用状态、分割线等 */
  --hnui-neutral-color-7: #f0f2f5;

  /* 最低颜色等 */
  --hnui-neutral-color-8: #fafbfc;

  /* #endregion vars-NeutralColor */

  /* 默认文字颜色 */
  --hnui-text-color: var(--hnui-neutral-color-2);

  --hnui-info-bg: #eceff4;
  --hnui-info-bg2: #e7e7e7;

  --hnui-color-white: #fff;
  --hnui-color-black: #000;

  --hnui-border-width: 1px;
  --hnui-border-dashed-width: 2px;
  --hnui-border-color: var(--hnui-neutral-color-6);

  --hnui-disabled-bg-color: var(--hnui-neutral-color-7);
  --hnui-disabled-color: var(--hnui-neutral-color-5);

  --hnui-control-height: 32px;
  --hnui-control-height-sm: 24px;
  --hnui-control-height-lg: 40px;

  --hnui-control-transparent: transparent;
}

.dark {
  --hnui-brand-color6: rgb(50, 59, 74);
  --hnui-brand-color7: #30353d;
  --hnui-brand-color8: #42484f;
  --hnui-brand-color9: #4c4f54;
  --hnui-brand-color10: #29292c;

  /* 中性色 */

  /* 标题文字 */
  --hnui-neutral-color-1: #c4c8dc;

  /* 正文、重要信息文字、按钮文字、输入框正常状态文字等 */
  --hnui-neutral-color-2: #b9bed3;

  /* 内容文字、说明叙述文字 */
  --hnui-neutral-color-3: #afb5c7;

  /* 提示性文字、辅助文本 */
  --hnui-neutral-color-4: #6a6f75;

  /* 禁用文本 */
  --hnui-neutral-color-5: #53565b;

  /* 描边格描边、输入文本描边、下拉选择描边等 */
  --hnui-neutral-color-6: #59595a;

  /* 全局背景、禁用状态、分割线等 */
  --hnui-neutral-color-7: #343535;

  /* 最低颜色等 */
  --hnui-neutral-color-8: #151516;

  --hnui-info-bg: #393b3d;

  --hnui-color-white: #000;
  --hnui-color-black: #fff;

  --hnui-control-transparent: transparent;
}

*,
::before,
::after {
  box-sizing: border-box;
}

body {
  color: var(--hnui-neutral-color-2);
}

/* #region vars-FontFamily */
@font-face {
  font-family: 'DIN';
  src: url('/fonts/D-DIN-PRO/D-DIN-PRO.woff2') format('woff2'),
    url('/fonts/D-DIN-PRO/D-DIN-PRO.woff') format('woff'),
    url('/fonts/D-DIN-PRO/D-DIN-PRO.ttf') format('truetype'),
    url('/fonts/D-DIN-PRO/D-DIN-PRO.eot') format('embedded-opentype'),
    url('/fonts/D-DIN-PRO/D-DIN-PRO.svg') format('svg');
  font-weight: normal;
  font-style: normal;
}

/* 使用数字字体 */
.use-font {
  font-family: DIN;
}
/* #endregion vars-FontFamily */

/* #region vars-BoxShadow */
/* 一级投影(默认向下) */
.hnui-box-shadow {
  box-shadow: 0px 6px 16px 6px rgba(0, 0, 0, 0.18);
}
.hnui-box-shadow.hover,
.hnui-box-shadow:hover {
  box-shadow: 0px 6px 16px 6px rgba(31, 120, 255, 0.18);
}

/* 二级投影(默认向下) */
.hnui-box-shadow_l2 {
  box-shadow: 0px 6px 16px 6px rgba(0, 0, 0, 0.12);
}
.hnui-box-shadow_l2.hover,
.hnui-box-shadow_l2:hover {
  box-shadow: 0px 6px 16px 6px rgba(31, 120, 255, 0.12);
}

/* 三级投影(默认向下) */
.hnui-box-shadow_l3 {
  box-shadow: 0px 6px 16px 6px rgba(0, 0, 0, 0.06);
}
.hnui-box-shadow_l3.hover,
.hnui-box-shadow_l3:hover {
  box-shadow: 0px 6px 16px 6px rgba(31, 120, 255, 0.06);
}
/* #endregion vars-BoxShadow */
css

/* 基础开关容器 */
.hnui-switch {
  /* 尺寸变量 */
  --hnui-switch-width: 32px;
  --hnui-switch-height: 20px;
  --hnui-switch-handle-size: 16px;
  --hnui-switch-handle-offset: 2px;

  /* 颜色变量 */
  --hnui-switch-bg-off: var(--hnui-neutral-color-6);
  --hnui-switch-bg-on: var(--hnui-brand-color-normal);
  --hnui-switch-handle-color: var(--hnui-color-white);

  position: relative;
  display: inline-block;
  width: var(--hnui-switch-width);
  height: var(--hnui-switch-height);
  background-color: var(--hnui-switch-bg-off);
  border-radius: calc(var(--hnui-switch-height) / 2);
  cursor: pointer;
  transition: background-color 0.2s cubic-bezier(0.38, 0, 0.24, 1);
  user-select: none;
  box-sizing: border-box;
}

/* 开关圆钮 */
.hnui-switch::after {
  content: "";
  position: absolute;
  top: var(--hnui-switch-handle-offset);
  left: var(--hnui-switch-handle-offset);
  width: var(--hnui-switch-handle-size);
  height: var(--hnui-switch-handle-size);
  background-color: var(--hnui-switch-handle-color);
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 4px 5px rgba(0, 0, 0, 0.08),
    0 1px 10px rgba(0, 0, 0, 0.05);
  transition: left 0.2s cubic-bezier(0.38, 0, 0.24, 1);
}

/* 未选中状态(默认) */
.hnui-switch.hnui-switch-off {
  background-color: var(--hnui-switch-bg-off);
}

.hnui-switch.hnui-switch-off:hover {
  background-color: var(--hnui-neutral-color-5);
}

/* 选中状态 */
.hnui-switch.hnui-switch-on {
  background-color: var(--hnui-switch-bg-on);
}

.hnui-switch.hnui-switch-on:hover {
  background-color: var(--hnui-brand-color-hover);
}

.hnui-switch.hnui-switch-on::after {
  left: calc(
    var(--hnui-switch-width) - var(--hnui-switch-handle-size) -
      var(--hnui-switch-handle-offset)
  );
}

/* 小号开关 */
.hnui-switch-sm {
  --hnui-switch-width: 26px;
  --hnui-switch-height: 16px;
  --hnui-switch-handle-size: 12px;
  --hnui-switch-handle-offset: 2px;
}

/* 大号开关 */
.hnui-switch-lg {
  --hnui-switch-width: 39px;
  --hnui-switch-height: 24px;
  --hnui-switch-handle-size: 18px;
  --hnui-switch-handle-offset: 3px;
}

/* 加载状态 */
.hnui-switch.hnui-switch-loading {
  cursor: not-allowed;
  pointer-events: none;
}

.hnui-switch.hnui-switch-loading.hnui-switch-off {
  background-color: var(--hnui-neutral-color-6);
}

.hnui-switch.hnui-switch-loading.hnui-switch-on {
  background-color: var(--hnui-brand-color6);
}

/* 加载图标容器 - 显示在圆钮中心 */
.hnui-switch-loading-icon {
  position: absolute;
  top: var(--hnui-switch-handle-offset);
  width: var(--hnui-switch-handle-size);
  height: var(--hnui-switch-handle-size);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  pointer-events: none;
}

/* 未选中时加载图标在左侧(与圆钮重叠) */
.hnui-switch.hnui-switch-loading.hnui-switch-off .hnui-switch-loading-icon {
  left: var(--hnui-switch-handle-offset);
}

/* 选中时加载图标在右侧(与圆钮重叠) */
.hnui-switch.hnui-switch-loading.hnui-switch-on .hnui-switch-loading-icon {
  left: calc(
    var(--hnui-switch-width) - var(--hnui-switch-handle-size) -
      var(--hnui-switch-handle-offset)
  );
}

/* 加载图标样式 */
.hnui-switch-loading-spinner {
  width: 12px;
  height: 12px;
  display: block;
  animation: hnui-switch-loading-rotate 0.8s linear infinite;
}

@keyframes hnui-switch-loading-rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 禁用状态 */
.hnui-switch.hnui-switch-disabled {
  cursor: not-allowed;
  opacity: 1;
}

.hnui-switch.hnui-switch-disabled:hover {
  background-color: inherit;
}

.hnui-switch.hnui-switch-disabled.hnui-switch-off {
  background-color: var(--hnui-neutral-color-7);
}

.hnui-switch.hnui-switch-disabled.hnui-switch-on {
  background-color: var(--hnui-brand-color7);
}

.hnui-switch.hnui-switch-disabled::after {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
}

基础开关

查看代码
html
<div class="hnui-switch hnui-switch-off"></div>
<div class="hnui-switch hnui-switch-on"></div>

开关大小

小尺寸
中尺寸
大尺寸
查看代码
html
<div class="hnui-switch hnui-switch-sm hnui-switch-off"></div>
<div class="hnui-switch hnui-switch-sm hnui-switch-on"></div>
html
<div class="hnui-switch hnui-switch-off"></div>
<div class="hnui-switch hnui-switch-on"></div>
html
<div class="hnui-switch hnui-switch-lg hnui-switch-off"></div>
<div class="hnui-switch hnui-switch-lg hnui-switch-on"></div>

开关状态

默认
加载状态
禁用状态
查看代码
html
<div class="hnui-switch hnui-switch-off"></div>
<div class="hnui-switch hnui-switch-on"></div>
html
<div class="hnui-switch hnui-switch-off hnui-switch-loading">
  <div class="hnui-switch-loading-icon">
    <svg class="hnui-switch-loading-spinner" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
      <path d="M 8 2 A 6 6 0 0 1 14 8" stroke="#1F78FF" stroke-width="2.5" stroke-linecap="round" fill="none" />
    </svg>
  </div>
</div>
<div class="hnui-switch hnui-switch-on hnui-switch-loading">
  <div class="hnui-switch-loading-icon">
    <svg class="hnui-switch-loading-spinner" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
      <path d="M 8 2 A 6 6 0 0 1 14 8" stroke="#1F78FF" stroke-width="2.5" stroke-linecap="round" fill="none" />
    </svg>
  </div>
</div>
html
<div class="hnui-switch hnui-switch-off hnui-switch-disabled"></div>
<div class="hnui-switch hnui-switch-on hnui-switch-disabled"></div>

统一UI