使用javascript动画css转换

时间:2013-07-14 22:11:49

标签: javascript css-transitions css-animations

我有一个特定的问题和一般性问题。

我必须使用javascript(没有框架)动画拖动,我认为使用translate()而不是更改top和left更好,至少为了更好的性能。

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

这是我的问题。

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

我想先了解它,所以没有框架。

感谢。

1 个答案:

答案 0 :(得分:1)

我看到了伊利亚关于矩阵的评论,这可能是一件好事。鉴于我在矩阵数学方面苦苦挣扎,基于字符串的解决方案就在我的小巷里。

我在大约半小时内把它扔在一起,但似乎运作良好。我假设您只是捕获鼠标移动并更新翻译值。我创建了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