如何在javascript中加载之前更改图像尺寸

时间:2018-03-26 11:31:15

标签: javascript

我正在遍历包含图像路径的列表,正如我所做的那样,我打算在加载之前更改长度和高度。

function loadEvent(evt, type) {
    var obj = { "page": "Gallery", "tab": type };
    var dbParam = JSON.stringify(obj);
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {

        if (this.readyState == 4 && this.status == 200) {
            var myObj = JSON.parse(this.responseText);
            var elems = document.getElementsByClassName(type);
            var imgCounter = 0;
            size = elems.length;

            for (var i = 0; i < myObj.length; i++) {
                inner: for (var j = 0; j < size; j++) {
                    if (imgCounter === j) {
                        var col = elems[j];
                        var img = document.createElement('img');
                        var poll = setInterval(function () {
                            if (img.naturalWidth) {
                                clearInterval(poll);
                                img.width = img.naturalWidth / 10;
                                img.height = img.naturalHeight / 10;
                            }
                        }, 10);
                        img.src = myObj[i].ImageLocation;
                        img.alt = "";
                        // img.width = getPngDimensions(myObj[i].Image).width/200000;
                        // img.height = getPngDimensions(myObj[i].Image).height/200000;

                        col.appendChild(img);

                        if (j === 3) {
                            imgCounter = 0;
                            break inner;
                        } else {
                            imgCounter++;
                            break inner;
                        }
                    }
                }
            };
        };
    };
    xmlhttp.open("GET", "../php/getGalleryImages.php?x=" + dbParam, true);
    xmlhttp.send();
}

我尝试通过poll变量实现一种改变宽度和高度的方法,但它只适用于其中一个图像而不是正在加载的15个

0 个答案:

没有答案