变换上的 CSS3 动画:旋转.获取旋转元素当前度数的

CSS3 animation on transform: rotate. Way to fetch current deg of the rotating element?(变换上的 CSS3 动画:旋转.获取旋转元素当前度数的方法?)
本文介绍了变换上的 CSS3 动画:旋转.获取旋转元素当前度数的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用拖放的 html5 界面.当我拖动一个元素时,目标获得了一个 css 类,这使得它由于 -webkit-animation 而双向旋转.

i am working on a html5 interface wich uses drag and drop. While i am dragging an element, the target gets a css-class, which makes it bidirectionally rotate due to a -webkit-animation.

@-webkit-keyframes pulse {
   0%   { -webkit-transform: rotate(0deg);  }
   25%  { -webkit-transform:rotate(-10deg); }
   75%  { -webkit-transform: rotate(10deg); }
   100% { -webkit-transform: rotate(0deg);  }
  }

.drag
{
    -webkit-animation-name: pulse;
    -webkit-animation-duration: 1s;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-timing-function: ease-in-out;
}

当我放下目标时,我希望它采用当前的旋转状态.

When I drop the target, I want it to adopt the current state of rotation.

我的第一个想法是使用 jquery 和 .css('-webkit-transform') 方法检查 css 属性.但是这个方法只返回'none'.

My first thought was to check the css property with jquery and the .css('-webkit-transform') method. But this method just returns 'none'.

所以我的问题是:有没有办法获取通过动画旋转的元素的当前度值?

So my question: Is there a way to get the current degree value of an element which is rotated via animation?

到目前为止谢谢亨德里克

Thanks so far Hendrik

推荐答案

我最近不得不写一个函数来做你想要的!随意使用它:

I recently had to write a function that does exactly what you want! Feel free to use it:

// Parameter element should be a DOM Element object.
// Returns the rotation of the element in degrees.
function getRotationDegrees(element) {
    // get the computed style object for the element
    var style = window.getComputedStyle(element);
    // this string will be in the form 'matrix(a, b, c, d, tx, ty)'
    var transformString = style['-webkit-transform']
                       || style['-moz-transform']
                       || style['transform'] ;
    if (!transformString || transformString == 'none')
        return 0;
    var splits = transformString.split(',');
    // parse the string to get a and b
    var parenLoc = splits[0].indexOf('(');
    var a = parseFloat(splits[0].substr(parenLoc+1));
    var b = parseFloat(splits[1]);
    // doing atan2 on b, a will give you the angle in radians
    var rad = Math.atan2(b, a);
    var deg = 180 * rad / Math.PI;
    // instead of having values from -180 to 180, get 0 to 360
    if (deg < 0) deg += 360;
    return deg;
}

希望这会有所帮助!

编辑我更新了代码以使用 matrix3d 字符串,但它仍然只给出 2d 旋转度数(即围绕 Z 轴旋转).

EDIT I updated the code to work with matrix3d strings, but it still only gives the 2d rotation degrees (ie. rotation around the Z axis).

这篇关于变换上的 CSS3 动画:旋转.获取旋转元素当前度数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

how to remove this error quot;Response must contain an array at quot; . quot;.quot; while making dropdown(如何删除此错误quot;响应必须在quot;处包含数组。创建下拉菜单时(Q;))
Why is it necessary to use `document.createElementNS` when adding `svg` tags to an HTML document via JS?(为什么在通过JS为一个HTML文档添加`svg`标签时,需要使用`Document.createElementNS`?)
wkhtmltopdf print-media-type uses @media print ONLY and ignores the rest(Wkhtmltopdf print-media-type仅使用@media print,而忽略其余内容)
price depend on selection of radio input(价格取决于无线电输入的选择)
calculate price depend on selection without button(根据没有按钮的选择计算价格)
What should I consider before minifying HTML?(在缩小HTML之前,我应该考虑什么?)