第一个切换开关未显示

时间:2018-06-08 05:27:12

标签: css

enter image description here

当我执行此代码时,第一个切换开关未显示,但第二个切换开关正常显示并且也正常工作。我不知道代码出错了。请帮帮我

.switch input {
  display: none;
}

.switch {
  display: inline-block;
  width: 60px;
  height: 30px;
  margin: 8px;
  transform: translateY(50%);
  position: relative;
}

.slider {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 30px;
  box-shadow: 0 0 0 2px #777, 0 0 4px #777;
  cursor: pointer;
  border: 4px solid transparent;
  overflow: hidden;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  background: #777;
  border-radius: 30px;
  transform: translateX(-30px);
  transition: .4s;
}

input:checked+.slider:before {
  transform: translateX(30px);
  background: limeGreen;
}

input:checked+.slider {
  box-shadow: 0 0 0 2px limeGreen, 0 0 2px limeGreen;
}
<div>
  <label class="switch">
                <input type="checkbox" value="Maths" name="Maths>
                <span class="slider"></span>
            </label> Maths
</div>
<div>
  <label class="switch">
                <input type="checkbox" name="Science" value="Science">
                <span class="slider"></span>
            </label> Science
</div>

第一个应与第二个一起显示。

3 个答案:

答案 0 :(得分:1)

你错过了“name="Maths之后

应该是:

<div>
    <label class="switch">
        <input type="checkbox" value="Maths" name="Maths">
        <span class="slider"></span>
    </label>
    Maths
</div>
<div>
    <label class="switch">
        <input type="checkbox" name="Science" value="Science">
        <span class="slider"></span>
    </label>
    Science
</div>

答案 1 :(得分:0)

name="Mathsname="Maths"。您错过了"。检查一下。

   .switch input { 
    display:none;
}
.switch {
    display:inline-block;
    width:60px;
    height:30px;
    margin:8px;
    transform:translateY(50%);
    position:relative;
}

.slider {
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    border-radius:30px;
    box-shadow:0 0 0 2px #777, 0 0 4px #777;
    cursor:pointer;
    border:4px solid transparent;
    overflow:hidden;
     transition:.4s;
}
.slider:before {
    position:absolute;
    content:"";
    width:100%;
    height:100%;
    background:#777;
    border-radius:30px;
    transform:translateX(-30px);
    transition:.4s;
}

input:checked + .slider:before {
    transform:translateX(30px);
    background:limeGreen;
}
input:checked + .slider {
    box-shadow:0 0 0 2px limeGreen,0 0 2px limeGreen;
}
<div>
        <label class="switch">
            <input type="checkbox" value="Maths" name="Maths">
            <span class="slider"></span>
        </label>
        Maths
    </div>
    <div>
        <label class="switch">
            <input type="checkbox" name="Science" value="Science">
            <span class="slider"></span>
        </label>
        Science
    </div>

答案 2 :(得分:0)

您刚收到拼写错误,即在name="Maths

之后缺少倒置的逗号

.switch input {
  display: none;
}

.switch {
  display: inline-block;
  width: 60px;
  height: 30px;
  margin: 8px;
  transform: translateY(50%);
  position: relative;
}

.slider {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 30px;
  box-shadow: 0 0 0 2px #777, 0 0 4px #777;
  cursor: pointer;
  border: 4px solid transparent;
  overflow: hidden;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  background: #777;
  border-radius: 30px;
  transform: translateX(-30px);
  transition: .4s;
}

input:checked+.slider:before {
  transform: translateX(30px);
  background: limeGreen;
}

input:checked+.slider {
  box-shadow: 0 0 0 2px limeGreen, 0 0 2px limeGreen;
}
<div>
  <label class="switch">
                <input type="checkbox" value="Maths" name="Maths">
                <span class="slider"></span>
            </label> Maths
</div>
<div>
  <label class="switch">
                <input type="checkbox" name="Science" value="Science">
                <span class="slider"></span>
            </label> Science
</div>