css过渡不按预期工作

时间:2014-03-16 15:26:18

标签: css transform translate3d

我想使用过渡来获得平滑的动画。在附加打开类的代码中(通过单击链接切换)效果是完美的但是当我们删除.opened时,翻译是即时的。

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

1 个答案:

答案 0 :(得分:5)

您应该将转换置于.bar

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

因为只要删除.opened,转移也会被移除。

相关问题