【问题标题】:animating css transformations with javascript用 javascript 动画 css 转换
【发布时间】:2013-07-15 08:17:36
【问题描述】:

我有一个具体问题和一般问题。

我必须使用 javascript(无框架)对拖动进行动画处理,并且我认为最好使用 translate() 而不是更改顶部和左侧,至少可以获得更好的性能。

但它是用户驱动界面的一部分,所以我无法预定义任何动画。所以我必须构建一个具有更新值的字符串并将其分配给元素样式属性。

这是我的问题。

因此,如果我必须为一些用户驱动的转换设置动画,我必须执行相对繁重的字符串操作来更新必要的值并保留那些我没有设置动画的值?有没有更简单的 API 可以用 javascript 来实现?

我想先了解一下,所以请不要框架。

谢谢。

【问题讨论】:

  • 你在搬什么?是否涉及重绘?如果不只是改变顶部和左侧。仅在必要时进行优化。
  • JS 中没有动画 api ..如果你想像这样动画,你基本上只需将 css 分配给元素的 .style 。
  • 所有 CSS 变换都可以用矩阵形式表示。可能在某些情况下,使用 JS 更改矩阵系数比将复合“变换”值构建为字符串更容易。在 CSS 变换矩阵上有一个很好的 introductory article(也有 translated into Russian:),将变换字符串转换为矩阵并返回的数学描述为 in the spec。

标签: javascript css-transitions css-animations


【解决方案1】:

我看到了 Ilya 关于矩阵的评论,这可能是件好事。鉴于我在矩阵数学方面遇到困难,基于字符串的解决方案正合我意。

我在大约半小时内将它拼凑在一起,但它似乎运作良好。我假设您只会捕获鼠标移动并更新翻译值。我创建了 toObject 方法以防你想做一些更高级的动画。

我认为如果你打算走这条路,你应该重新考虑不想使用框架。

(function() {
    var setTransFunc, getTransFunc;

    // make sure cross browser...
    // I don't know if this is nescessary.  Every browser I tried worked on the first options.
    (function () {
        var testElem = document.createElement('DIV');
        if (testElem.style.transform !== undefined) {
            setTransFunc = function (elem, str) {
                elem.style.transform = str;
            }
            getTransFunc = function (elem) {
                return elem.style.transform;
            }
        }
        else if (testElem.style.webkitTransform !== undefined) {
            setTransFunc = function (elem, str) {
                elem.style.webkitTransform = str;
            }
            getTransFunc = function (elem) {
                return elem.style.webkitTransform;
            }
        }
        else if (testElem.style.msTransform !== undefined) {
            setTransFunc = function (elem, str) {
                elem.style.msTransform = str;
            }
            getTransFunc = function (elem) {
                return elem.style.msTransform;
            }
        }
        else {
            throw ('unable to detect set/get methods for transform style.');
        }
    }).call();

    // constructor
    var _tranZ = function (elem) {
        this.elem = (typeof elem == 'string') ? document.getElementById(elem) : elem;
    };

    (function () {
        this.elem = null;
        // sets transform style
        this.set = function (str) {
            setTransFunc(this.elem, str);
            return this;
        }
        // gets string of transform style
        this.get = function () {
            return getTransFunc(this.elem);
        }
        // adds a trasnform
        this.add = function (str) {
            this.set(this.get() + ' ' + str);
            return this;
        }
        // removes a transform
        this.remove = function (name) {
            var re = new RegExp(name + "\\([^)]*\\)", "gi");
            console.log(re);
            this.set(this.get().replace(re, ""));
            return this;
        }
        // represent transforms as object.  Might be easier later on to animate with this.
        this.getTransformObject = function () {
            var str = this.get(),
                re = /(\w+)\s*\(([^)]*)\)/g,
                match, obj = {};

            while (match = re.exec(str)) {
                obj[match[1]] = {
                    parameters: match[2].split(/\s*,\s*/)
                }
            }

            return obj;
        }
    }).call(_tranZ.prototype);

    // add a window module
    window.tranZ = function (elem) {
        return new _tranZ(elem);
    };
})();

jsFiddle

【讨论】:

    猜你喜欢
    • 2015-01-25
    • 2021-11-07
    • 2021-05-17
    • 1970-01-01
    • 2021-11-08
    • 2016-09-18
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多