在切换按钮中添加动画



我正在尝试使用 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>

转换

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

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

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>

试试这个切换器

.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>

你可以试试这个。或访问此链接了解详情

.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; 
}
<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>

相关内容

  • 没有找到相关文章

最新更新