<摘要>中使用切换按钮

时间:2019-08-29 21:04:52

标签: html

我正在尝试在<summary>元素中放置一个切换按钮,使用此处的切换按钮:https://www.w3schools.com/howto/howto_css_switch.asp

在Firefox上工作正常,但是在Chrome上,单击切换按钮实际上是切换<details>元素而不是切换按钮。如果我使用的是圆角版本,那么如果单击切换按钮的角(由于四舍五入而不再是灰色/蓝色的区域),则可以切换切换按钮,但是如果单击内部则不能。

我该怎么做才能使其在Chrome上运行?两种行为中的哪一种是“正确”的?

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<details>
  <summary>
    <label class="switch">
      <input type="checkbox">
      <span class="slider"></span>
    </label>
  </summary>
</details>

1 个答案:

答案 0 :(得分:0)

我的意思是,它并不是完全好的UX,但要使其保持简单和语义,只需将可操作元素及其所需的各自事件中的歧义分开。例如;

.switch-summary-container {
  position: relative;
  margin: 1rem;
}

.switch-summary-container aside {
  position: absolute;
  top: -.5rem;
  left: 1rem;
}

summary {
  min-height: 40px;
}

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<section class="switch-summary-container">
  <aside>
    <label class="switch">
      <input type="checkbox">
      <span class="slider"></span>
    </label>
  </aside>
  <details>
    <summary>
    </summary>
    Words Words Words.
  </details>
</section>