JavaSript
のプロパティをfor loop
に割り当てる方法が間違っているのですが、どうすればこの問題を解決できますか? first test
でなぜこのループの後に最後のIDの小道具しか得られないのですか?
var setImageSize = function (i) {
instances[i].img = $('<img id="jquery-background-zoom-'+i+'"/>');
var img = instances[i].img;
img.hide();
img.bind('load', function(e) {
var id = parseInt($(this).attr('id').replace('jquery-background-zoom-', ''));
instances[id].settings.bg.width = parseInt($(this).width());
instances[id].settings.bg.height = parseInt($(this).height());
$('#debug').html($('#debug').html() + '[' + i + ' ' + instances[id].settings.bg.width + ', ' + instances[id].settings.bg.height + ']<br>');
updateDefaultValues(instances[id]);
$(this).remove();
});
$('body').append(img);
img.attr('src', instances[i].settings.bg.url);
}
var setEvent = function (i) {
return function (e) {
var inst = instances[$(this).index()];
$('#debug').html(i + ' ' + inst.settings.bg.url);
$(this).bind('mousemove', {i:instances[$(this).index()]}, setFollowMouse);
}
}
var init = function (options) {
for (var i = 0; i < this_obj.length; i ++) {
instances.push ({id:i, element:this_obj.get(i), settings:$.extend(true, defaults, options)});
instances[i].settings.bg.url = $(this_obj.get(i)).css('background-image').replace('url(', '').replace(')', '').replace(/'/g, '').replace(/"/g, '');
$('#debug').html($('#debug').html() + '<br>' + i + ' -- ' + instances[i].toSource() + '<br><br>');
setImageSize (i);
$(instances[i].element).hover(setEvent(i), function() {
$(this).unbind('mousemove', setFollowMouse);
zoomOut($(this).index());
});
}
}
init(); // at the bottom of jQuery plug-in
私は私が期待するものを手に入れる:event test
で
({id:0, settings:{url:"http://localhost/js/img/example_0.jpg", other_propos:'relative to the id 0'}})
({id:1, settings:{url:"http://localhost/js/img/example_1.jpg", other_propos:'relative to the id 1'}})
({id:2, settings:{url:"http://localhost/js/img/example_2.jpg", other_propos:'relative to the id 2'}})
私はmousemove
イベントを呼び出したときに、私はなぜ、last element propos
しかし右id
のみを取得しますか?
({id:0, settings:{url:"http://localhost/js/img/example_2.jpg", other_propos:'relative to the id 2'}})
({id:1, settings:{url:"http://localhost/js/img/example_2.jpg", other_propos:'relative to the id 2'}})
({id:2, settings:{url:"http://localhost/js/img/example_2.jpg", other_propos:'relative to the id 2'}})
ここでは何が欠けているのか分かりませんが、どうすればこの問題を回避できますか?
私はこの問題は、それが本当のwidth
とheight
だ保存するために必要なimg
要素background image
を、ロードsetImageSize
方法で可能性が疑われます。
ロード遅延が発生する可能性はありますか?
forループで間違った小道具の割り当てについてテストが話しているので、わかりません。
ここはhttp://jsfiddle.net/tonino/CFPTa/問題はjquery loadイベントにあると確信していますが、解決方法はわかりません。
、私は考えていない '$(この).INDEXは()'あなたはそれをやっていると思う何をやっています。その呼び出しを 'i'で置き換えてください –
あなたは何か別のものを表示しています。たとえば、 'element:this_obj.get(i)'がありますが、 'first test'には表示されていません。 '' mousemove '、{i:インスタンスのタイプミス?あなたの例をhttp://jsfiddle.netに移動できましたか? – Cheery
'$(this).index()'は要素の現在のインデックスを返します。最初のインデックスが0の場合は0、2番目のインデックスの場合は以下同様に続きます。これは配列 'instances'のように動作するでしょうか? – vitto