我有一个切换按钮+折叠的代码...需要帮助结合两者

时间:2019-01-11 09:18:25

标签: html css

我这里有一个切换动画:https://codepen.io/7harrypotterrr/pen/OrBwPY,但不知道如何将其实际应用到我的代码中,并在这里替换现有的切换按钮:https://codepen.io/7harrypotterrr/pen/ebPKjZ

.toggle-box {
  display: none;
}

.toggle-box + label {
  cursor: pointer;
  display: block;
  font-weight: bold;
  line-height: 21px;
  margin-bottom: 5px;
}

.toggle-box + label + div {
  display: none;
  margin-bottom: 10px;
}

.toggle-box:checked + label + div {
  display: block;
}

.toggle-box + label:before {
  background-color: #4F5150;
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  border-radius: 10px;
  color: #FFFFFF;
  content: "+";
  display: block;
  float: left;
  font-weight: bold;
  height: 20px;
  line-height: 20px;
  margin-right: 5px;
  text-align: center;
  width: 20px;
}

.toggle-box:checked + label:before {
  content: "\2212";
}

.checkbox:hover {
    color: #0da1ec !important;
}

label:hover {
  color: pink;
}
<div id="page">

  <input class="toggle-box" id="identifier-1" type="checkbox">
  <label for="identifier-1">test</label>
  <div>test</div>
  <input class="toggle-box" id="identifier-2" type="checkbox">
  <label for="identifier-2">test</label>
  <div>test</div>
</div>

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:0)

您可以忽略当前的CSS,并在第一个CodePen中应用CSS来实现所需的样式。

您还需要在每个标签后删除div,并用<i></i>替换'test'。

label {
  display: block;
  width: 54px;
  height: 32px;
  margin: 0px auto;
  border-radius: 100px;
  transition: all 0.2s ease-in-out;
  -webkit-transition: all 0.2s ease-in-out;
  background-color: #E6E9EC;
}

input {
  display: none;
}


/* The toggle */

i {
  height: 28px;
  width: 28px;
  background: #ffffff;
  display: inline-block;
  border-radius: 100px;
  margin-top: 2px;
  margin-left: 2px;
  transition: all 0.2s ease-in-out;
  -webkit-transition: all 0.2s ease-in-out;
  pointer-events: none;
  box-shadow: 0px 0px 0px 0px rgba(0, 0, 0, 0);
}

label:hover>i {
  box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.20);
  transform: scale(1.01);
}

input:checked+label>i {
  margin-left: 24px;
}

label:active {
  background-color: #A6B9CB;
}

label:active>i {
  width: 34px;
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.20);
}

input:checked+label:active>i {
  margin-left: 18px;
}

input:checked+label {
  background-color: #008FFF;
}
<div id="page">

  <input class="toggle-box" id="identifier-1" type="checkbox">
  <label for="identifier-1"><i></i></label>

  <input class="toggle-box" id="identifier-2" type="checkbox">
  <label for="identifier-2"><i></i></label>

</div>

答案 1 :(得分:0)

如果按照代码段构造HTML,以下CSS代码应该可以实现所需的功能。

.toggly + label + div {
  display: none;
  margin-bottom: 10px;
}

.toggly:checked + label + div {
  display: block;
}

label {
  display: block;
  width: 54px;
  height: 32px;
  margin: 0px auto;
  border-radius: 100px;
  transition: all 0.2s ease-in-out;
  -webkit-transition: all 0.2s ease-in-out;
  background-color: #E6E9EC;
}

input {
  display: none;
}


/* The toggle */

i {
  height: 28px;
  width: 28px;
  background: #ffffff;
  display: inline-block;
  border-radius: 100px;
  margin-top: 2px;
  margin-left: 2px;
  transition: all 0.2s ease-in-out;
  -webkit-transition: all 0.2s ease-in-out;
  pointer-events: none;
  box-shadow: 0px 0px 0px 0px rgba(0, 0, 0, 0);
}

label:hover>i {
  box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.20);
  transform: scale(1.01);
}

input:checked+label>i {
  margin-left: 24px;
}

label:active {
  background-color: #A6B9CB;
}

label:active>i {
  width: 34px;
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.20);
}

input:checked+label:active>i {
  margin-left: 18px;
}

input:checked+label {
  background-color: #008FFF;
}



.toggly + label + div {
  display: none;
  margin-bottom: 10px;
}

.toggly:checked + label + div {
  display: block;
}
<input type="checkbox" id="toggly" class="toggly">
  <label for="toggly"><i></i></label>
<div>Test</test>