在切换按钮中添加动画

时间:2018-03-15 04:46:42

标签: html css css3 css-transitions

我正在尝试使用HTML元素创建一个切换按钮。但我不能为此做动画。在这里,我尝试使用transition CSS3属性。转换仅适用于某些元素,并且不会像switch-group:before元素那样工作。有没有办法改进更多的动画来获得一个有吸引力的动画?我尝试了很多链接,但这对我的代码没有帮助。我在下面附上了代码,

input[type=checkbox] {
  display: none;
}

.switch-wrapper {
  position: relative;
  width: 70px;
  border: 1px solid;
  cursor: pointer;
  height: 22px;
  overflow: hidden;
}

.switch-on,
.switch-off {
  display: block;
  width: 50%;
  float: left;
  height: 100%;
  transition: 0.5s;
}

.switch-on {
  background-color: red;
  margin-left: -100%;
  text-indent: -18px;
  line-height: 21px;
  text-align: center;
}

.switch-off {
  text-indent: 18px;
  line-height: 21px;
  text-align: center;
  background-color: aliceblue;
}

.switch-group:before {
  display: block;
  content: "";
  position: absolute;
  height: 18px;
  width: 18px;
  margin: 2px;
  background-color: #1b191e;
  top: 0px;
}

.switch-group {
  display: block;
  position: relative;
  height: 23px;
  width: 200%;
  user-select: none;
}

input[type=checkbox]:checked+.switch-group .switch-on {
  margin-left: 0;
}

input[type=checkbox]:checked+.switch-group:before {
  right: 50%;
}
<div class="switch-wrapper">
  <input type="checkbox" id="checked" />
  <label class="switch-group" for="checked">
    <span class="switch-on">ON</span>
    <span class="switch-off">OFF</span>
  </label>
</div>

3 个答案:

答案 0 :(得分:5)

转换不适用于您的:before伪元素,因为您要将:before right值从auto(默认情况下)更改为0。 ..transition不适用于auto值......

因此,请尝试在开始时使用left:0,然后使用calc()在点击时(输入时选中)更改其值,并使用transtion:0.5s中的:before将其设为动画

input[type=checkbox] {
  display: none;
}

.switch-wrapper {
  position: relative;
  width: 70px;
  border: 1px solid;
  cursor: pointer;
  height: 22px;
  overflow: hidden;
}

.switch-on,
.switch-off {
  display: block;
  width: 50%;
  float: left;
  height: 100%;
  transition: 0.5s;
}

.switch-on {
  background-color: red;
  margin-left: -50%;
  text-indent: -18px;
  line-height: 21px;
  text-align: center;
}

.switch-off {
  text-indent: 18px;
  line-height: 21px;
  text-align: center;
  background-color: aliceblue;
}

.switch-group:before {
  display: block;
  content: "";
  position: absolute;
  height: 18px;
  width: 18px;
  margin: 2px;
  background-color: #1b191e;
  top: 0px;
  left: 0;
  transition: 0.5s;
}

.switch-group {
  display: block;
  position: relative;
  height: 23px;
  width: 200%;
  user-select: none;
}

input[type=checkbox]:checked+.switch-group .switch-on {
  margin-left: 0;
}

input[type=checkbox]:checked+.switch-group:before {
  left: calc(50% - 22px);
}
<div class="switch-wrapper">
  <input type="checkbox" id="checked" />
  <label class="switch-group" for="checked">
    <span class="switch-on">ON</span>
    <span class="switch-off">OFF</span>
  </label>
</div>

或者,如果您希望代码以更直观的方式工作,请尝试使用opacity代替margin

我已经改变了一些你的css

input[type=checkbox] {
  display: none;
}

.switch-wrapper {
  position: relative;
  width: 70px;
  border: 1px solid;
  cursor: pointer;
  height: 22px;
  overflow: hidden;
}

.switch-on,
.switch-off {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  transition: 0.5s;
  padding: 2px 6px;
}

.switch-on {
  background-color: red;
  line-height: 21px;
  opacity: 0;
}

.switch-off {
  line-height: 21px;
  background-color: aliceblue;
  opacity: 1;
  text-align: right;
}

.switch-group:before {
  display: block;
  content: "";
  position: absolute;
  height: 18px;
  width: 18px;
  margin: 2px;
  background-color: #1b191e;
  top: 0px;
  left: 0;
  transition: 0.5s;
  z-index: 99;
}

.switch-group {
  display: block;
  position: relative;
  height: 23px;
  user-select: none;
}

input[type=checkbox]:checked+.switch-group .switch-on {
  opacity: 1;
}

input[type=checkbox]:checked+.switch-group .switch-off {
  opacity: 0;
}

input[type=checkbox]:checked+.switch-group:before {
  left: calc(100% - 22px);
}
<div class="switch-wrapper">
  <input type="checkbox" id="checked" />
  <label class="switch-group" for="checked">
    <span class="switch-on">ON</span>
    <span class="switch-off">OFF</span>
  </label>
</div>

答案 1 :(得分:2)

试试这个toggler

.onoffswitch {
    position: relative; width: 48px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    height: 24px; padding: 0; line-height: 24px;
    border: 2px solid #F1F1F5; border-radius: 24px;
    background-color: #F1F1F5;
    transition: background-color 0.3s ease-in;
}
.onoffswitch-label:before {
    content: "";
    display: block; width: 24px; margin: 0px;
    background: #FFFFFF;
    position: absolute; top: 0; bottom: 0;
    right: 22px;
    border: 2px solid #F1F1F5; border-radius: 24px;
    transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label {
    background-color: #2DC76D;
}
.onoffswitch-checkbox:checked + .onoffswitch-label, .onoffswitch-checkbox:checked + .onoffswitch-label:before {
   border-color: #2DC76D;
}
.onoffswitch-checkbox:checked + .onoffswitch-label:before {
    right: 0px;
}

.onoffswitch-checkbox:checked + .onoffswitch-label span.switch-off { 
	opacity: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label span.switch-on{
	opacity: 1;
}
span.switch-off {
    opacity: 1;
    float: right;
    font-size: 12px;
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
}
span.switch-on {
    position: absolute;
    font-size: 12px;
    top: 50%;
    transform: translateY(-50%);
    opacity: 0;
}
<div class="onoffswitch">
            <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
            <label class="onoffswitch-label" for="myonoffswitch">
            
            <span class="switch-on">ON</span>
            <span class="switch-off">OFF</span></label>
        </div>

答案 2 :(得分:2)

您可以试试这个..或visit This link For Details

&#13;
&#13;
.onoffswitch {
    position: relative; width: 90px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #999999; border-radius: 20px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 30px; padding: 0; line-height: 30px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "ON";
    padding-left: 10px;
    background-color: #34A7C1; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "OFF";
    padding-right: 10px;
    background-color: #EEEEEE; color: #999999;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 18px; margin: 6px;
    background: #FFFFFF;
    position: absolute; top: 0; bottom: 0;
    right: 56px;
    border: 2px solid #999999; border-radius: 20px;
    transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}
&#13;
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
    <label class="onoffswitch-label" for="myonoffswitch">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>
&#13;
&#13;
&#13;