我正在使用模式窗口在縮略圖上顯示圖片。
但是,調整大小的照片沒有被加載到模態窗口中。在執行其他代碼之前完成加載圖像
圖像的src最初並不在我的標記中,但是我通過在標籤上添加它來將它添加到標記中。我第二次點擊它,圖片加載到模態中。
查看控制檯時,我能夠第一次獲得高度和寬度。第二次,我得到的高度和寬度加上「圖像加載」文本。
我想檢查照片是否已加載,如果已完成,請執行其餘代碼。如果未加載,請繼續檢查,直至加載完成,然後繼續執行其餘代碼。
我已閱讀關於堆棧溢出的其他文章,但他們都沒有爲我工作。 有些人可以幫忙。
$(document).ready(function() {
function galleryStart ($current) {
var $tImage = $current.find('img');
var fullURL = $tImage.attr('src');
var $dFigure = $('#dialog-media figure .media');
var fullSrcURL = "images/mypicture";
var fullSrcURL = fullURL.replace(".jpg", "_full.jpg");
var image;
function onload() {
console.log(image.height, image.width);
var objHeight = image.height;
var objWidth = image.width;
}
image= new Image();
image.src=fullSrcURL;
if (image.complete) {
console.log('image already loaded to browser');
onload();
} else {
image.onload = onload;
console.log('xxx');
}
$dFigure.append('<img id="dialog-media-object" src="' + fullSrcURL + '" alt="' + $tImage.attr('alt') + '" />');
//Execute more code here once the image has finished loading
//More code to load the image into the modal window
}
$('a.dialog-media').click(function (event) {
event.preventDefault();
$("#dialog").dialog({
height: "auto", //720
width: "auto", //960
draggable: false,
modal: true,
//position: {
// my: "center",
// at: "center",
// of: window },
open: function(event, ui){
galleryStart(tLink);
},
close: function(event, ui){
galleryEnd();
$(this).dialog('destroy').remove();
});
}); //最終收盤標籤
在這 - '$(」 #dialog-media figure .media');''figure'是什麼?應該是'$('#dialog-media-figure .media');'而不是? – Archer
@Archer - 'figure'是一個新的HTML5標籤。 – Steve
謝謝@Steve。不幸的是,我不得不迎合HTML5以前的版本,所以還沒有機會使用它。我是一個壞的方式復古:( – Archer