简体   繁体   English

动画后旋转div

[英]Rotate div after animation

I'm trying to create a sort of crash test dummy, when you click in the document window my .person div will move right and then at the end animate tilt forward, and then tilt back to its original place. 我正在尝试创建一种崩溃测试假人,当您在document窗口中单击时,我的.person div将向右移动,然后在最后使动画向前倾斜,然后向后倾斜到其原始位置。

I've got the css for the transform working, only its tilting my div immediately and not at the end of the movement? 我已经为转换工作使用了CSS,只是它立即使div倾斜而不是在运动结束时倾斜? sorry if this is hard to understand, I've created a jsFiddle to explain... 抱歉,如果这很难理解,我创建了一个jsFiddle来解释...

http://jsfiddle.net/4mzg8/3/ http://jsfiddle.net/4mzg8/3/

// On click my .person div will move right, once moved it needs to tilt forward and then backwards - as though its hit a wall...

$(document).on('click',function(){
    $('.person').animate({ "left": "+=250px" }, 500, crashImpact(30));
});

function crashImpact(tilt) {
    $('.person').css({
        transform: 'rotate(' + tilt + 'deg)'
    });
}

It needs a little work, but you can do the whole thing in CSS just by adding a class and chaining animations... 它需要一点工作,但是您只需添加类并链接动画即可完成CSS的全部工作。

Demo Fiddle - (Chrome) 演示小提琴 -(Chrome)

JS JS

$(document).on('click', function () {
    $('.person').addClass('go');
});

CSS CSS

.person.go {
    -webkit-animation-delay:0, .5s;
    -webkit-animation-duration:.5s, .5s;
    -webkit-animation-iteration-count:1, 1;
    -webkit-animation-name:animForward, animTilt;
    -webkit-animation-timing-function:ease-in;
    -webkit-animation-fill-mode:forwards;
    -webkit-transform-origin: 0%;
}
@-webkit-keyframes animForward {
    from {
        margin-left: 0;
    }
    to {
        margin-left: 400px;
    }
}
@-webkit-keyframes animTilt {
    0% {
        -webkit-transform:rotate(0deg);
    }
    50% {
        -webkit-transform:rotate(45deg);
    }
    100% {
        -webkit-transform:rotate(0deg);
    }
}
$(document).on('click',function(){
    $('.person').animate({ "left": "+=250px" },500,function(){
        crashImpact(30);
    });
});

function crashImpact(tilt) {
    $('.person').css({
        'transform': 'rotate(' + tilt + 'deg)'
    });
}

I modified your code. 我修改了您的代码。 Check here : http://jsfiddle.net/lotusgodkk/4mzg8/4/ 检查这里: http : //jsfiddle.net/lotusgodkk/4mzg8/4/

You have written: 你写:

$('.person').animate({ "left": "+=250px" }, 500, crashImpact(30));

This will call crashImpact function immediately. 这将立即调用crashImpact function

Change it to: 更改为:

$('.person').animate({ "left": "+=250px" },500,function(){
    crashImpact(30);
});

This will call crashImpact function after execution of animate is completed. animate执行完成后,这将调用crashImpact function

transition property will rotate .person smoothly. transition属性将使.person平稳旋转。

Write: 写:

CSS: CSS:

.transform{
    transition:all 1s;
    -webkit-transition:all 1s;
}

JS: JS:

function crashImpact(tilt) {
    $('.person').css({
        'transform': 'rotate(' + tilt + 'deg)'
    }).addClass("transform");
}

Updated fiddle here 在这里更新小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM