是否可以在 javascript 类中插入 jquery 函数?例如,我有以下 JS 类:
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 两个图像作为背景,具有:
$(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);
});