get @keyframe current value in css3 with javascript

巧了我就是萌 提交于 2019-12-06 07:16:50

问题


See the demo here: http://jsfiddle.net/hamidrezabstn/fgcPa/5/

When I click on the middle raindrop , I would like it to rotate to the current position of the spinning circle! I tried below the JS code but it doesn't work! The next thing I want to do is the raindrop rotate with spining circle!

 $(function() {
    $('#center').click(function() {
        var pos = $('#circle').css('transform')
        $(this).css('transform', 'pos')

        });
});

回答1:


EDITED: I said that it is not posible to get the current status of the transform in an animation, but I was wrong. Sorry about that !

To do what you want, any way, you don't need really to get it; just use it.

I have changed slightly your HTML to put the raindrop inside the rotating div.

Then, with this CSS:

.raindrop {
    background:center blue;
    width:50px;
    height:50px;
    border-radius: 100%;
    border-top-left-radius: 0;
    position: absolute;
    margin: auto;    
    left: 75px;
    top: 75px;
    animation: ccircle 5s infinite linear;
    -webkit-animation: ccircle 5s infinite linear;
}

.raindrop:hover {
    animation: none;
    -webkit-animation: none;

}

.axis {
    width: 200px;
    height: 200px;
    transform: scaleX(2);
    background-color: none;
    border: 1px solid black;
    left: 50px;
    position: absolute;
    top: 50px;
    border-radius: 50%;
}
.rotate {
    width: 100%;
    height: 100%;
    top: 0px;
    animation: circle 5s infinite linear;
    -webkit-animation: circle 5s infinite linear;
    transform-origin: 50% 50%;
    -webkit-transform-origin: 50% 50%;
    position: absolute;
}
.counterrotate {
    width: 50px;
    height: 50px;
    animation: ccircle 5s infinite linear;
    -webkit-animation: ccircle 5s infinite linear;
}
.planet {
    width: 50px;
    height: 50px;
    position: absolute;
    border-radius : 50px;
    left: 0px;
    top: 0px;
    background-color: red;
    display: block;
}
@keyframes circle {
    from {   transform: rotateZ(0deg)    }
    to {        transform: rotateZ(360deg)    }
}
@-webkit-keyframes circle {
    0% {   -webkit-transform: rotateZ(0deg)    }
    100% {        -webkit-transform: rotateZ(360deg)    }
}
@keyframes ccircle {
    from {        transform: rotateZ(360deg)    }
    to {        transform: rotateZ(0deg)    }
}
@-webkit-keyframes ccircle {
    from { -webkit-transform: rotateZ(360deg) }
    to   { -webkit-transform: rotateZ(0deg)   }
}

You got this fiddle

In it, the raindrop is always rotating with the axis div. But it is also counter-rotating, so it appears to be static.

When you hover it, the count-rotation is disabled, and it points to red circle. And will continue to do so as long as you hover it.

To do that thru a click, just asociate the :hover properties to a class, and set this class in the click.




回答2:


  $(function() {
    $('#center').click(function() {
        var obj, matrix;
        obj = document.getElementById('circle');
        matrix = getComputedStyle(obj).getPropertyValue('transform');
        console.log(matrix);
        //$(this).css('transform', matrix)

        });
});   

read more here http://dev.opera.com/articles/view/understanding-3d-transforms/



来源:https://stackoverflow.com/questions/18426306/get-keyframe-current-value-in-css3-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!