是否有可能在jQuery類中插入jquery函數? 例如我有以下JS類:作爲一個JavaScript類方法的jquery方法
function FloatingImage() {
var delay, duration;
var moveRight = function($image)
{
$image.delay(delay).animate(
{
left: $image.parent().width() - $image.width()
},
{
duration: duration,
complete: function(){ moveLeft($image) }
});
},
moveLeft = function($image){
$image.delay(delay).animate({
left: 0
}, {
duration: duration,
complete: function(){ moveRight($image) }
});
};
this.constructor = function (delay, duration) {
this.delay = delay;
this.duration = duration;
};
}
下列支持功能:
function rand(l,u) // lower bound and upper bound
{
return Math.floor((Math.random() * (u-l+1))+l);
}
,然後調用它,假設有2周的div #imgleft和#imgright與兩個2個圖像作爲背景與:
$(function(){
var $imageL = $('#imgleft'),
$imageR = $('#imgright');
var fi1 = new FloatingImage();
fi1.constructor(rand(400,600), rand(1500,3000));
var fi2 = new FloatingImage();
fi2.constructor(rand(400,600), rand(1500,3000));
fi1.moveRight($imageL);
fi2.moveLeft($imageR);
});
你試過了嗎?你是否收到任何錯誤或意外的行爲?是的,你可以在JavaScript構造函數或原型中包含jQuery。 – apsillers
我不明白你的問題,jQuery是Javascript代碼。你能確定它嗎?您想做什麼 ?你究竟在做什麼?你試過了什麼?你會得到什麼錯誤?順便說一下,Javascript中沒有「class」 – pomeh
@pomeh對不起,不清楚。我想移動2個圖像,像這樣:http://jsfiddle.net/linuxatico/XnPjL/ 但我想重新組織我的代碼,以便我可以快速分配不同的延遲和持續時間 – linuxatico