Подтвердить что ты не робот

Изображение

image naturalWidth возвращает ноль... что это, почему?

var newimage = new Image();
newimage.src = 'retouche-hr' + newlinkimage.substring(14,17) + '-a.jpg'; 
var width = newimage.naturalWidth;
alert (width);

ПОМОЩЬ, я не знаю, почему!

*** этот путь хорош, изображение появляется!

4b9b3361

Ответ 1

Я бы предположил, потому что вы не дождались загрузки изображения - попробуйте следующее:

var newimage = new Image();
newimage.src = 'retouche-hr' + newlinkimage.substring(14,17) + '-a.jpg'; 
newimage.onload = function()
{
    var width = this.naturalWidth;
    alert(width);
}

Ответ 2

вот ЗАКЛЮЧИТЕЛЬНЫЙ РАБОЧИЙ КОД... на случай, если кто-то захочет узнать, все это связано с тем, что все изображения загружены!...

<script type="text/javascript">

$(function() {   $ ( "# Палец" ). JCarouselLite ({       btnNext: "#down",       btnPrev: "#up",       vertical: true,       видимый: 4   });

$("#thumb li img").click(function() {

    var newlinkimage = $(this).attr("src");
    newlinkimage = 'retouche-hr' + newlinkimage.substring(14,17);

    $('#hr').remove();

    var newimage = new Image();
    newimage.src = newlinkimage + '-a.jpg';

    newimage.onload = function()
    {
        var width = (newimage.width);
        var height = (newimage.height);

        $('#contentfull').append('<div id="hr"> </div>');
        $("#hr").attr("width", width).attr("height", height);
        $("#hr").addClass('hrviewer');
        //alert('a');
        $("#hr").append('<div id="avant"> <img alt="before" src="' + newlinkimage +'-a.jpg"></div>');
        $("#hr").append('<div id="apres"> <img alt="after" src="' + newlinkimage +'-b.jpg"></div>');
        //alert('b');
        $("#avant img").attr("src", newlinkimage + '-a.jpg').attr("width", width).attr("height", height);
        $("#apres img").attr("src", newlinkimage + '-b.jpg').attr("width", width).attr("height", height);

        $("#apres img").load(function(){$("#hr").beforeAfter({animateIntro:true});}); 

    }

})

});

Ответ 3

Свойства NaturalWidth и naturalHeight не поддерживаются в IE, поэтому дайте это исправление попробовать.

Ответ 4

Для меня следующие работы...

    $('<img src="mypathtotheimage.png"/>').load(function () {

        if (!isNotIe8) {
            // ie8 need a fix
            var image = new Image(); // or document.createElement('img')
            var width, height;
            image.onload = function () {
                width = this.width;
                height = this.height;
            };
            image.src = $(this).attr("src");

        } else {
            // everythings fine here ....
            // this.naturalWidth / this.naturalHeight;
        }