Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

left transition not working both ways (works left to right but not the other way around)

I use position relative and absolute on the switch and handle respectively, then use left on the handle to move it to the end or start of ‘cSwitch’ but the transition duration i applied only works from left to right.

i tried applying right too but that didn’t change anything.

const cSwitch = document.querySelectorAll('.cSwitch')[0];

cSwitch.onclick = () => {
  
  (cSwitch.getAttribute('state') === 'on') ? cSwitch.setAttribute('state', 'off') : cSwitch.setAttribute('state', 'on');

}
.cSwitch{
    
    position: relative;
    
    width: 150px;
    aspect-ratio: 2/1;
    
    border-radius: 50px 50px;
  
  background: crimson;
    
}

.cSwitch[state = on] .cSwitch_handle{
    
    position: absolute;
    
    left: 50%;
    
}

.cSwitch_handle{
    
    width: 75px;
    aspect-ratio: 1;
    
    border-radius: 50%;
    
    background: skyblue;
    
    left: 0%;
    
    transition: left 0.5s;
    
}
<div class="switch-wrapper">
                            
    <div class="cSwitch" state="off" id="neon-switch">
         <div class="cSwitch_handle"></div>
    </div>
                        
</div>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You neglected to apply position: absolute in the default state, so left has no effect. left may transition from 50% back to 0%– but it doesn’t apply, because the element is not positioned.

Put position: absolute into the rule with the .cSwitch_handle selector instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading