即時通訊使用畫布創建一些圖像的動畫,但我需要存儲原始寬度和高度供以後使用(如均勻縮放它們)jquery .data不能正常工作?
但做測試(最後警報顯示未定義)似乎.data不工作有人可以告訴我我做錯了什麼?
的Jquery:
$(document).ready(function(){
// Creacion de imagenes animadas y sonidos
var gun = $("#gun")[0];
var alarm = $("#alarm")[0];
$('canvas.animado').each(function(index, element){
var obj = $(this);
var canvas = $(this)[0];
var context = element.getContext('2d');
var img = new Image();
img.src = $(this).data('url');
img.onload = function() {
canvas.width = img.width/2;
canvas.height = img.height;
context.drawImage(img, 0, 0);
obj.data('width', 'prueba');
};
$(this).on({
"mouseover" : function() {
$(this).css('opacity', '0.3');
canvas.width = canvas.width;
context.drawImage(img, img.width/2,0,img.width/2,img.height,0,0,img.width/2,img.height);
$(this).fadeTo('slow', 1);
gun.play();
},
"mouseout" : function() {
$(this).css('opacity', '0.5');
canvas.width = canvas.width;
context.drawImage(img, 0, 0);
$(this).fadeTo('slow', 1);
}
});
});
// Correccion de heroes
alert($('#teniente').data('width'));
});
HTML:
<ul id="heroes">
<li class="pedestal">
<canvas id="aguila" class="animado img-responsive" data-url="images/sh_aguila.png"></canvas>
</li>
<li class="pedestal">
<canvas id="justice" class="animado img-responsive" data-url="images/sh_justice.png"></canvas>
</li>
<li class="pedestal">
<canvas id="teniente" class="animado img-responsive" data-url="images/sh_teniente.png"></canvas>
</li>
<li class="pedestal">
<canvas id="ninja" class="animado img-responsive" data-url="images/sh_ninja.png"></canvas>
</li>
<li class="pedestal">
<canvas id="ghost" class="animado img-responsive" data-url="images/sh_gosth.png"></canvas>
</li>
</ul>
在圖片出現之前或警報之後裝?由於在圖像「加載」之前您沒有設置「寬度」數據屬性,因此您可能會在設置寬度之前嘗試提醒寬度。 –