【问题标题】:HTML/CSS YES/NO Slider shows more fields when yes is selectedHTML/CSS YES/NO 滑块在选择是时显示更多字段
【发布时间】:2019-02-24 02:06:36
【问题描述】:

我对 Web 开发比较陌生,如果能得到任何帮助,我将不胜感激。我有一个 HTML/CSS 滑块,当滑块处于“是”位置时,我想显示更多 txt 框字段,例如名字和主题。如果可能的话,我真的希望这些选项基于另一个下拉菜单。我该怎么做?

.switch {
  position: relative;
  display: block;
  vertical-align: top;
  width: 100px;
  height: 30px;
  padding: 3px;
  margin: 0 10px 10px 0;
  background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px);
  background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px);
  border-radius: 18px;
  box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05);
  cursor: pointer;
}

.switch-input {
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
}

.switch-label {
  position: relative;
  display: block;
  height: inherit;
  font-size: 10px;
  text-transform: uppercase;
  background: #eceeef;
  border-radius: inherit;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
}

.switch-label:before,
.switch-label:after {
  position: absolute;
  top: 50%;
  margin-top: -.5em;
  line-height: 1;
  -webkit-transition: inherit;
  -moz-transition: inherit;
  -o-transition: inherit;
  transition: inherit;
}

.switch-label:before {
  content: attr(data-off);
  right: 11px;
  color: #aaaaaa;
  text-shadow: 0 1px rgba(255, 255, 255, 0.5);
}

.switch-label:after {
  content: attr(data-on);
  left: 11px;
  color: #FFFFFF;
  text-shadow: 0 1px rgba(0, 0, 0, 0.2);
  opacity: 0;
}

.switch-input:checked~.switch-label {
  background: #0088cc;
  border-color: #0088cc;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2);
}

.switch-input:checked~.switch-label:before {
  opacity: 0;
}

.switch-input:checked~.switch-label:after {
  opacity: 1;
}

.switch-handle {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 28px;
  height: 28px;
  background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0);
  background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0);
  border-radius: 100%;
  box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
}

.switch-handle:before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -6px 0 0 -6px;
  width: 12px;
  height: 12px;
  background: linear-gradient(to bottom, #eeeeee, #FFFFFF);
  background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF);
  border-radius: 6px;
  box-shadow: inset 0 1px rgba(0, 0, 0, 0.02);
}

.switch-input:checked~.switch-handle {
  left: 74px;
  box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2);
}


/* Transition

========================== */

.switch-label,
.switch-handle {
  transition: All 0.3s ease;
  -webkit-transition: All 0.3s ease;
  -moz-transition: All 0.3s ease;
  -o-transition: All 0.3s ease;
}
<div class="field">
  <label>send an email?</label>
  <label class="switch">
<input class="switch-input" type="checkbox" id="email" name="email" value=" 
{{values.email}}"/>
<span class="switch-label" data-on="Yes" data-off="No"></span>
<span class="switch-handle"></span>
</label>

【问题讨论】:

    标签: html css web-applications slider web-deployment


    【解决方案1】:

    如果您使用label,您有两个选项可以使标签有效。一种是将input 包裹在label 中,如下所示:

    <label> Label text
      <input type="text … />
    </label>
    

    另一种选择是使用label上的for属性,这意味着当点击标签时,与for的值匹配的id被点击。

    <input type="checkbox" id="myCheckbox" name="myCheckbox" value=" true" />
    <label for="myCheckbox">Click the checkbox</label>
    

    这是您应该为不带for 属性的非包装label 使用的选项。目前无效。

    另一个label(.switch)封装了开关的UI,并没有失效。但是,我们可以使用for 属性另外 来包装开关内容,使两个labels 指向同一个id。这是labels 的混合方法,有效HTML

    我们为什么要这样做?因为要显示更多内容,我们需要将包含目标 id 的复选框在 DOM 中“向上”移动一个级别。完成此操作后,我们可以使用~ 选择器根据复选框的值显示/隐藏更多内容。

    .switch {
      position: relative;
      display: block;
      vertical-align: top;
      width: 100px;
      height: 30px;
      padding: 3px;
      margin: 0 10px 10px 0;
      background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px);
      background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px);
      border-radius: 18px;
      box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05);
      cursor: pointer;
    }
    
    .switch-input {
      position: absolute;
      top: 0;
      left: 0;
      opacity: 0;
    }
    
    .switch-label {
      position: relative;
      display: block;
      height: inherit;
      font-size: 10px;
      text-transform: uppercase;
      background: #eceeef;
      border-radius: inherit;
      box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
    }
    
    .switch-label:before,
    .switch-label:after {
      position: absolute;
      top: 50%;
      margin-top: -.5em;
      line-height: 1;
      -webkit-transition: inherit;
      -moz-transition: inherit;
      -o-transition: inherit;
      transition: inherit;
    }
    
    .switch-label:before {
      content: attr(data-off);
      right: 11px;
      color: #aaaaaa;
      text-shadow: 0 1px rgba(255, 255, 255, 0.5);
    }
    
    .switch-label:after {
      content: attr(data-on);
      left: 11px;
      color: #FFFFFF;
      text-shadow: 0 1px rgba(0, 0, 0, 0.2);
      opacity: 0;
    }
    
    .switch-input:checked~.switch .switch-label {
      background: #0088cc;
      border-color: #0088cc;
      box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2);
    }
    
    .switch-input:checked~.switch .switch-label:before {
      opacity: 0;
    }
    
    .switch-input:checked~.switch .switch-label:after {
      opacity: 1;
    }
    
    .switch-handle {
      position: absolute;
      top: 4px;
      left: 4px;
      width: 28px;
      height: 28px;
      background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0);
      background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0);
      border-radius: 100%;
      box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
    }
    
    .switch-handle:before {
      content: "";
      position: absolute;
      top: 50%;
      left: 50%;
      margin: -6px 0 0 -6px;
      width: 12px;
      height: 12px;
      background: linear-gradient(to bottom, #eeeeee, #FFFFFF);
      background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF);
      border-radius: 6px;
      box-shadow: inset 0 1px rgba(0, 0, 0, 0.02);
    }
    
    .switch-input:checked~.switch .switch-handle {
      left: 74px;
      box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2);
    }
    
    /* Transition
    
    ========================== */
    
    .switch-label,
    .switch-handle {
      transition: All 0.3s ease;
      -webkit-transition: All 0.3s ease;
      -moz-transition: All 0.3s ease;
      -o-transition: All 0.3s ease;
    }
    
    .show-more-fields {
      display: none;
    }
    
    .switch-input:checked~.show-more-fields {
      display: block;
    }
    <div class="field">
      <input class="switch-input" type="checkbox" id="email" name="email" value=" true" />
      <label for="email">send an email?</label>
      <label for="email" class="switch">
        <span class="switch-label" data-on="Yes" data-off="No"></span>
        <span class="switch-handle"></span>
      </label>
      <div class="show-more-fields">More content here</div>
    </div>

    jsFiddle

    【讨论】:

    • 这太棒了,谢谢!如果我希望 show-more-fields 根据下拉菜单中选择的值显示某些字段,我需要做什么?
    • @Zone188 在这种情况下,您需要将&lt;select&gt; 附加到onchange 事件并手动检查某些&lt;option&gt; 值。没有纯粹的CSS 方式来实现这一点。
    • 我更改了 jsFiddle,在该示例中,如果选择了 Pizza 选项,我只希望 Pizza Toppings 字段显示。我该怎么做?
    • 你能和我分享你的 jsFiddle 链接吗?
    • @Zone188 给你(新的jsFiddle)。我在CSS 的底部添加了一些JavaScript 和一些新规则。如果您对此感到满意,如果您接受我的回答,我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2019-10-02
    • 2015-01-02
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多