Hello. Already I suffer the second day in search of an answer to the behavior of js. In attempts to write data inside onload , the current image data is there, but I don’t understand why, I tried to create objects there, copy and convert data - I couldn’t work anything, I didn’t understand everything everywhere. Maybe one of you knows the answer. Thanks in advance http://jsfiddle.net/r2bzoL9r/
$(window).on("resize load",function(){ var img = new Image(); img.onload = function(){ D['IW'] = img.width, D['IH'] = img.height; } img.src = url; }) full code:
(function($) { jQuery.fn.myF = function(options) { var data = {}; //создаю глобальный объект со всеми данными var make = function(indexEL) { var this_o = $(this); //в глобальном объекте создаю под каждый элемент обьект с его данными data['el ' + indexEL] = { IH: 00, IW: 00, toString: function() { return 'IH : ' + this.IH + '<br>\ IW : ' + this.IW + '<br>\ url : ' + this.url + '<br>' } } D = data['el ' + indexEL] //просто для краткости объект элемента который сейчас идет по циклу //записываю в объект элемента путь картинки D['url'] = this_o.css('background-image') //а вот записать высоту с шириной не выходит почему не знаю $(window).on("resize load", function() { var img = new Image(); img.onload = function() { D['IW'] = img.width, D['IH'] = img.height; } img.src = url; }) //вывожу на элементе его данные this_o.find(".info").html(D + '') }; //цикл по найденым элементам return this.each(make); }; })(jQuery); $('.img').myF({}) .img { height: 200px; width: 200px; background: #543; margin: 10px; } #gg1 { background-image: url("http://www.porjati.ru/uploads/posts/2013-09/thumbs/1380204950_5.jpg"); } #gg3 { background-image: url("http://www.radionetplus.ru/uploads/posts/2013-04/1365401196_teplye-oboi-1.jpeg"); } #gg2 { background-image: url("http://picfun.ru/wp-content/uploads/HTxyUcwXfw.jpg"); } .info { color: #000; background: #f4f; opacity: .8; font-size: 10px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='img' id='gg1'> <div class='info'> </div> </div> <div class='img' id='gg2'> <div class='info'> </div> </div> <div class='img' id='gg3'> <div class='info'> </div> </div>
Dbefore the image data is written to it? - br3timg.src='url("http://www.radionetplus.ru/uploads/posts/2013-04/1365401196_teplye-oboi-1.jpeg")'. Incorrect example or implementation? - br3t