【问题标题】:Please explain the meaning of $({deg: 0}) in JQuery请解释一下JQuery中$({deg: 0})的含义
【发布时间】:2015-04-03 22:41:41
【问题描述】:

谁能解释一下CSS rotation cross browser with jquery.animate()中的$({deg: 0})是什么意思?

例如

$({deg: 0}).animate({deg: angle}, {
    duration: 2000,
    step: function(now) {
        // in the step-callback (that is fired each step of the animation),
        // you can use the `now` paramter which contains the current
        // animation-position (`0` up to `angle`)
        $elem.css({
            transform: 'rotate(' + now + 'deg)'
        });
    }
});

如果您能向我展示相关的 jQuery 官方文档,我将不胜感激。

【问题讨论】:

  • 我不太了解这里的否决票,似乎是合理的问题,恕我直言
  • 我很惊讶以前没有人问过这个问题(我在 SO 上看到过),但它有点晦涩难懂。好问题:)

标签: jquery


【解决方案1】:

它创建了一个 disconnected 伪 jQuery 对象,其属性 deg 设置为 0

你可能以前见过这个:

var newDiv = $('<div>', {class: "hello"});

这会创建一个特定的元素类型并在其上设置初始属性。没有指定元素类型也是一样的。

注意:这种类型的对象对于插入 DOM 是无效的,但是您可以对其应用许多有用的 jQuery 方法。

因此,您可以使用来自该对象的信息来做一些很酷的事情:http://jsfiddle.net/TrueBlueAussie/cfmzau1w/

// Create a pseudo jQuery object (not connected to a DOM element)
var po = $({
    deg: 0
});

// This is just the target object we want to change
$elem = $('#result');

// Use animate on the object, to make use of the step callback
po.animate({
    deg: 360
}, {
    duration: 5000,
    step: function (now) {
        // Take the computed value and use it on a real element!
        $elem.css({
            transform: 'rotate(' + now + 'deg)'
        });
    }
});

参考并不明显,但在这个页面上http://api.jquery.com/jquery/#jQuery-object 它有一个jQuery(object) 方法说:

jQuery(对象)
对象类型:PlainObject
包装在 jQuery 对象中的普通对象。

在您的示例中,object 是您的匿名对象,因此示例的 long-hand 伪代码类似于:

var anonymousObj = {
     deg: 0
};
var wrappedInAjQueryObject = jQuery(anonymousObj);
wrappedInAjQueryObject.animate(targetPropertiesAndValues, someSettingsAndCallbacksInAnObject);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 2022-09-30
    • 2011-11-08
    • 1970-01-01
    • 2012-03-11
    • 2017-08-09
    • 1970-01-01
    相关资源
    最近更新 更多