javascript - jquery图片加载函数

标签 javascript jquery image load

// Called when image is dropped in canvas
function dropResource() {

    var imgIndex = getImageIndexByID(currentDragImageID);
    var newImgID = resourceData.length;
    var newImage;

    newImage = $('<div id="imgD' + newImgID + '" style="display:inline-block;position:absolute;"><img alt="Big" id="imgA' + newImgID + '"  src="' + uploadFolder + '/' + imgData[imgIndex][1] + '" /></div>');
    $('#thePage').append(newImage);

    $('imgA' + newImgID).load(function() {

        // Get properties
        var imgW = $('#imgA' + newImgID).width();           // Native width store this
        var imgH = $('#imgA' + newImgID).height();          // Native
        var imgX = $('#imgA' + newImgID).position().left;   // Relative, scale back 
        var imgY = $('#imgA' + newImgID).position().top;
        var zindex = getBiggestZindex() + 1;

        // Resize native dimensions to fit current scale
        $('#imgA' + newImgID).width(Math.round(imgW * currentScale));
        $('#imgA' + newImgID).height(Math.round(imgH * currentScale));
        $('#imgA' + newImgID).css("z-index", zindex);

        // Add to array (dbID, imgarrIndex, width, height, x, y)
        resourceData[newImgID] = new Array(0, imgIndex, imgW, imgH, imgX, imgY, zindex);

        // Make img wrapper draggable
        makeResourceDrag(newImgID);

        // Save this as a resource
        $.ajax({
            url: 'artworkAjaxHandler.ashx?type=addResource&uploadID=' + currentDragImageID + '&page=' + currentPage + '&w=' + imgW + '&h=' + imgH + '&x=' + imgX + '&y=' + imgY + '&z=' + zindex,
            success: function(data) {
                var splitData = data.toString().split("|");

                // Success
                if (splitData[0] == "1") {

                } else {
                    $.fancybox.close();
                    $.jGrowl("<strong>Error during image drop! Try reloading page.</strong><br />" + splitData[1], { sticky: true });
                }
            },
            error: function() {
                $.jGrowl("<strong>Error</strong><br />There was an error when processing the Ajax request", { sticky: true });
            }
        });
    });

加载函数永远不会调用... 我知道我可能没有正确执行此操作,但是一旦图像加载完成,它会自动调整包装器的大小然后我需要捕获维度。

谁能帮忙?

最佳答案

您缺少哈希:

 $('imgA' + newImgID).load(function() 

应该是

 $('#imgA' + newImgID).load(function() 

关于javascript - jquery图片加载函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4968306/

相关文章:

javascript - 如何使用 Redux Observable 进行 EventSource

javascript - 一次又一次点击后添加/隐藏一个div

asp.net - 通过按钮命令由服务器端调用客户端

javascript - ExtJS X模板

javascript - 在另一个 ajax 函数结束后执行操作

Javascript 变量不更新它的值

javascript - 打印对 AJAX 调用的响应

ios - Swift ios 是否可以使用 UIImageView 在图像之间滑动

javascript - 在 Canvas 上围绕图像的非透明部分绘制边框

html - 无法使用填充 css 向上推图像