jquery プラグインを作成していますが、対象となる要素ごとに変数を指定する必要があります。私の問題は、jsでこれを行うときです:
$("section ul").cards("init", {
current: 0
}).on("mousewheel", mousewheel);
function mousewheel(){
$(this).cards("next");
};
プラグインはconsole.log()、更新されcurrentた . しかし、彼は同じデータをすべて「ul」に更新します。
mousewheel最初のI のとき、それに対する電流は 1 (開始は 0) になり、2 番目のulI の直後と電流は 2 になります...mousewheelul
すべてのオブジェクトに独自の変数が必要です ( $(this).data())
ありがとう !
!! 編集 !!
私のプラグインベースがあります:
(function($){
$.fn.cartes = function( methode, options ){
if(options){
return methodes[methode].apply(this, $.makeArray(options));
}
else {
return methodes[methode].call(this);
};
};
var methodes = {
init: function( options ){
return this.each(function(){
var $this = $(this);
var Options = $.meta ? $.extend( $.fn.cartes.defauts, options, $this.data() ) : options;
$this.data("options", Options);
$this.each(function(index){
$this.data("options").rotation = $.fn.cartes.defauts.rotation;
$this.data("options").espacement = $.fn.cartes.defauts.espacement;
});
});
},
prec: function(obj){
return $(obj).each(function(){
$(this).data("options").cur--;
console.log( $(this).data("options").cur );
});
},
next: function(obj){
return $(obj).each(function(){
$(this).data("options").cur++;
console.log( $(this).data("options").cur );
});
}
};
$.fn.cartes.defauts = {
cur: 0,
espacement: 0,
rotation: -45
};
})(jQuery);
!! 編集2!!
each ループを使用するconsole.logと、mousewheelイベント内で適切なデータが返されます。
$("section li.double ul").each(function(){
$(this).cartes("init", {
actuel: 0,
selecteur: "li"
});
});
しかし、各ループなしでそれを行う方法。