【问题标题】:get @keyframe current value in css3 with javascript使用javascript在css3中获取@keyframe当前值
【发布时间】:2013-08-27 16:58:21
【问题描述】:

在此处查看演示: http://jsfiddle.net/hamidrezabstn/fgcPa/5/

当我点击中间的雨滴时,我希望它旋转到旋转圆圈的当前位置!我在JS代码下面尝试过,但它不起作用!接下来我要做的就是雨滴随着旋转的圆圈旋转!

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

        });
});

【问题讨论】:

  • 我没有最终的解决方案给你,但如果我正确理解你的问题,csstricks 将介绍如何获取动画元素的当前位置:Get Value of CSS Rotation through JavaScript。我还添加了 -webkit 前缀以使动画和转换工作:updated fiddle
  • @dc5 感谢您的重播!我做了每一件事,但它不起作用:(
  • @Bergi 这并不是真正的重复,因为该问题没有答案。 (即使有一个公认的答案,它也不起作用)
  • @vals:是的,但是getComputedStyle 的另一个答案确实有效

标签: javascript jquery css css-animations


【解决方案1】:
  $(function() {
    $('#center').click(function() {
        var obj, matrix;
        obj = document.getElementById('circle');
        matrix = getComputedStyle(obj).getPropertyValue('transform');
        console.log(matrix);
        //$(this).css('transform', matrix)

        });
});   

在此处阅读更多信息http://dev.opera.com/articles/view/understanding-3d-transforms/

【讨论】:

    【解决方案2】:

    已编辑:我说在动画中获取变换的当前状态是不可能的,但我错了。很抱歉!

    做你想做的事,无论如何,你不需要真的得到它;就用它吧。

    我稍微更改了您的 HTML 以将雨滴放在旋转的 div 中。

    然后,使用这个 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)   }
    }
    

    你收到了这个fiddle

    在其中,雨滴始终随着轴 div 旋转。但它也是反向旋转的,所以它看起来是静态的。

    当你悬停它时,计数旋转被禁用,它指向红色圆圈。只要您将其悬停,就会继续这样做。

    要通过点击来实现,只需将 :hover 属性关联到一个类,然后在点击中设置这个类。

    【讨论】:

    • 获得正在运行的动画的当前变换绝对是可能的 - 我链接的 css-tricks 文章描述了如何:Example(您需要更宽的屏幕)
    • @dc5 你是对的,我已经更正了我的答案。感谢您发现这一点!
    猜你喜欢
    • 2015-10-18
    • 2014-06-25
    • 1970-01-01
    • 2010-11-05
    相关资源
    最近更新 更多