Question

I want to use the transition to have a smooth animation. In the code when the class opened is appended (by clicking the link Toggle) the effect is perfect but when we remove the .opened the translate is instant.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>

    <style>
    .bar {
        background-color: yellow;
    }
    .opened .bar{
        transition: all ease 1s;
        -webkit-transform: translate3d(300px, 0, 0);
        transform: translate3d(300px, 0, 0);
    }
    </style>
</head>
<body>
    <div class="bar">
    <a href="#" id="toggle">Toggle</a>
    </div>

    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script>

    jQuery(document).ready(function($) {
        $('#toggle').on('click', function(e) {
            e.preventDefault();
            $('body').toggleClass('opened');
        });
    });
</script>
</body>
</html>
Was it helpful?

Solution

You should put the transition on .bar:

<style>
.bar {
    background-color: yellow;
    transition: all ease 1s;
}
.opened .bar{
    -webkit-transform: translate3d(300px, 0, 0);
    transform: translate3d(300px, 0, 0);
}
</style>

Because as soon as you remove .opened the transition is removed as well, before doing any transition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top