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があり、両方の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);
});