代码之家  ›  专栏  ›  技术社区  ›  Shanmugaraja_K

在切换按钮中添加动画

  •  1
  • Shanmugaraja_K  · 技术社区  · 7 年前

    我正在尝试使用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 回复  |  直到 7 年前
        1
  •  5
  •   Bhuwan    7 年前

    在您的 :before 伪元素,因为您正在更改 :之前 right 值来自 auto (默认情况下)至 0 。。。转换不起作用 汽车 价值观

    所以试着使用 left:0 在开始时,然后单击更改其值(选中输入时),使用 calc() 并使用 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=“交换机包装器”>
    <输入type=“checkbox”id=“checked”/>
    <标签class=“switch groupfor=“checked”>
    <span class=“打开”>打开(<)/跨度(>);
    <span class=“关闭”>关闭(<)/跨度(>);
    </标签>
    </div>
        2
  •  2
  •   Athul Nath    7 年前

    试试这个开关

    .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>
        3
  •  2
  •   Md. Abu Sayed    7 年前

    你可以试试这个。。或 visit This link For Details

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