0

Хочу передать значения высоты и ширины загружаемого изображения. До этого пытался делать это с помощью return, но выяснил, что это не работает и неправильно. После решил делать с помощью callback функции. Но появился вопрос - как передать значения высоты и ширины?

Как я пытлася получить значения с помощью return без callback:

popup: {
    dimentions: {},
    testFunc: function testFunc(url) {
         this.dimentions = this.getMeta(url);
         console.log(this.dimentions); //browser returns {} h: 1920 w: 720
         console.log(this.dimentions.w); //browser returns undefined

    },

    getMeta: function getMeta(url){
         var meta = {};
         var img = new Image();
         img.onload = function(){
              meta.w = this.width;
              meta.h = this.height;
         };
         img.src = url;
         return meta; //browser returns {} h: 1920 w: 720
    },
}

Как я хочу сделать это с помощью callback, но не знаю как получить значения:

testFunc: function testFunc(url) {
     this.getMeta(url, function() {
          this.dimensions = .... //how can i find dimensions from a callback function?
     });
     console.log("console log: " + this.dimentions);    
},

getMeta: function getMeta(url, cb){
     var meta = {};
     var img = new Image();
     img.onload = cb;
     img.src = url;
},

2 ответа 2

0
dimensions: {},
getMeta: function getMeta(url){
     var meta = this.dimensions;
     var img = new Image();
     img.onload = function(){
          meta.w = this.width;
          meta.h = this.height;
     };
     img.src = url;
},

var img = new Image();
document.body.appendChild(img);
img.onload = function() {
  console.log("In onload: " + img.width + " x " + img.height);
}
img.src = "https://assets.pernod-ricard.com/uk/media_images/test.jpg";
console.log("Immediately (picture hasn't arrived yet): " + img.width + " x " + img.height);

2
  • Всё равно undefined
    – Morbius
    15 окт 2018 в 19:28
  • Что "undefined"?
    – user176262
    15 окт 2018 в 19:30
0

Решил так:

resizePopup: function resizePopup(url) {

            this.getMeta(url, function(){
                ratio = gallery.popup.dimentions.w / gallery.popup.dimentions.h;
                if(ratio <= 1) {
                //console.log("console.log: " + popupWidth);
                content_height = $(".popup-item").height();
                content_width = content_height*ratio;
                    $(".popup-item").width(content_width + "px");
                } else {

                }
            });
        },

        getMeta: function getMeta(url, cb){
            //var meta = {};
            //meta = this.dimentions;
            var img = new Image();
            img.onload = function() {
                gallery.popup.dimentions.w = this.width;
                gallery.popup.dimentions.h = this.height;
                cb();                    
            }
            img.src = url;
        },
    },   

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.