CSS animation doesn't work changing top value

2019-08-15 17:59发布

问题:

With this JQuery I change the vertical position of the form, but I have ta problem with the animation: I would like to animate the change of the position, so the user can see the input field moving to the new position. Why doesn't the animation work?

http://jsfiddle.net/p6m97gwb/

If it is possible, I would like to use animate.css

HTML

<div id="searchMainWrapper">
    <div id="searchMain" class="vertical-align-middle">
        <form>
            <input type="text">
        </form>
    </div>
</div>

CSS

#searchMainWrapper {
    -webkit-animation: animate 3s; /* Safari and Chrome */
       -moz-animation: animate 3s; /* Firefox */
            animation: animate 3s;
    -webkit-transform-style: preserve-3d;
       -moz-transform-style: preserve-3d;
            transform-style: preserve-3d;

    position: absolute;
    width: 100%;
    height: 100%
}

.vertical-align-middle {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}
.vertical-align-top {
    position: relative;
    top: 0px;
}

JS

$('#searchMain').removeClass('vertical-align-middle').addClass('vertical-align-top');

回答1:

Use transition instead of animate

demo

#searchMain{
    position:absolute;
    transition: 3s;
}
.vertical-align-middle {
    top: 50%;
}
.vertical-align-top {
    top: 0px;
}