我正在创建一个jQuery插件,它可以动态地创建Raphael对象,假设您可以.
$("div").draw({
type: 'circle',
radius: '10',
color: '#000'
})以及插件代码(仅举一个例子):
$.fn.draw = function( options ) {
//settings/options stuff
var width = $(this).width();
var height = $(this).height();
var widget = Raphael($(this)[0], width, height);
var c = widget.circle(...).attr({...})
//saving the raphael reference in the element itself
$(this).data('raphael', {
circle : c
})
}但是,我希望能够更新这样的元素:
$("div").draw({
type: 'update',
radius: '20',
color: '#fff'
});我可以“拯救”执行$(this).data().raphael.circle的对象,但是它拒绝动画,我知道它是一个raphael对象,因为它甚至有动画proto,但是它产生了一个未定义的TypeError:无法读取属性'0‘)。
发布于 2011-04-24 18:52:55
我尝试了您的代码,做了一些修改,$(this).data().raphael.circle做了动画。这就是我所做的(我知道这和你的不完全一样,但给出了要点)
$.fn.draw = function( options ) {
//settings/options stuff
if(options.type === 'draw') {
var width = $(this).width();
var height = $(this).height();
var widget = Raphael($(this)[0], width, height);
var c = widget.circle(100, 100, 50);
//saving the raphael reference in the element itself
$(this).data('raphael', {
circle : c
});
//$(this).data().raphael.circle.animate() does not work here
}
else if(options.type === 'update') {
$(this).data().raphael.circle.animate({cx: 200, cy: 200});
//But this works here
}
}在本例中,使用$( this ).data().raphael.circle引用元素确实有效,但只在else if中工作。我不知道为什么。
https://stackoverflow.com/questions/5769630
复制相似问题