How can I make my CSS toggle state changing smoother e.g. with a transition?

I’ve created a toggle in CSS:

jQuery( document ).ready( function ( $ ) {
    $( document ).on( 'click', '.checkbox-toggle-wrapper .checkbox-toggle', function () {
        $( this ).toggleClass( 'enabled' );
    } )
} );
.checkbox-toggle-wrapper {
    margin-right: 12px;
    margin-top: 5px;
    margin-left: 1px;
}

.checkbox-toggle-wrapper .checkbox-toggle:not(.enabled)::before {
    right: auto;
    left: 0;
}

.checkbox-toggle-wrapper .checkbox-toggle:not(.enabled) {
    border-color: #999;
    background-color: #999;
}

.checkbox-toggle-wrapper .checkbox-toggle {
    height: 16px;
    width: 32px;
    border: 2px solid #00695c;
    background-color: #00695c;
    display: inline-block;
    text-indent: -9999px;
    border-radius: 10em;
    position: relative;
    margin-top: -1px;
    vertical-align: text-top;
    cursor: pointer;
}

.checkbox-toggle-wrapper .checkbox-toggle::before {
    content: "";
    display: block;
    width: 16px;
    height: 16px;
    background: #fff;
    position: absolute;
    top: 0;
    right: 0;
    border-radius: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="checkbox-toggle-wrapper">
  <span class="checkbox-toggle enabled"></span>
</div>

I’m looking now for a way to make everything a bit more smoother. I’ve tried out setting transition: transition: all .3s but this seems not to work. Any ideas?

29 thoughts on “How can I make my CSS toggle state changing smoother e.g. with a transition?”

  1. Use this instead:

    .checkbox-toggle-wrapper .checkbox-toggle:not(.enabled)::before {
      right: 100%;
      transform: translateX(100%);
    }
    

    To make sure you can apply transition to right and transform

    jQuery(document).ready(function($) {
      $(document).on('click', '.checkbox-toggle-wrapper .checkbox-toggle', function() {
        $(this).toggleClass('enabled');
      })
    });
    .checkbox-toggle-wrapper {
      margin-right: 12px;
      margin-top: 5px;
      margin-left: 1px;
    }
    
    .checkbox-toggle-wrapper .checkbox-toggle:not(.enabled)::before {
      right: 100%;
      transform: translateX(100%);
    }
    
    .checkbox-toggle-wrapper .checkbox-toggle:not(.enabled) {
      border-color: #999;
      background-color: #999;
    }
    
    .checkbox-toggle-wrapper .checkbox-toggle {
      height: 16px;
      width: 32px;
      border: 2px solid #00695c;
      background-color: #00695c;
      display: inline-block;
      text-indent: -9999px;
      border-radius: 10em;
      position: relative;
      margin-top: -1px;
      vertical-align: text-top;
      cursor: pointer;
      transition:0.3s all;
    }
    
    .checkbox-toggle-wrapper .checkbox-toggle::before {
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      background: #fff;
      position: absolute;
      top: 0;
      right: 0;
      border-radius: 100%;
      transition:0.3s all;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="checkbox-toggle-wrapper">
      <span class="checkbox-toggle enabled"></span>
    </div>
    Reply
  2. community bible church van nuys friends door frame , community college transfer. community history synonym buy lyrica online order Pregabalin , bridges community action union county ohio planning process journal individual level processes definition , community action council rental assistance positive affirmations pdf? community cast britta positive quotes zig ziglar, community health center yavapai social office.
    social anxiety test , community college gpa requirements environments always have both living and community college quincy il.

    Reply
  3. free office software upgrade drawing program website to buy autocad lt autocad electrical 2020 software free download. office software examples indesign software 2020 free download engeeeneringu#$sssaunnplus , 2020 design software customer service. latest office software for mac, free forum software 2020 what office software are you most comfortable using software engineer internship 2020. front office software names neosat i5000 new software 2020 download, 2020 kitchen design software download.

    Reply

Leave a Comment