首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >图像naturalWidth返回零

图像naturalWidth返回零
EN

Stack Overflow用户
提问于 2009-10-30 01:01:14
回答 3查看 27K关注 0票数 26

图像naturalWidth返回零...就是这样,为什么?

代码语言:javascript
复制
var newimage = new Image();
newimage.src = 'retouche-hr' + newlinkimage.substring(14,17) + '-a.jpg'; 
var width = newimage.naturalWidth;
alert (width);

救命,我不知道为什么!

*该路径很好,图像显示出来了!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-10-30 01:07:11

我猜这是因为你没有等待图像加载--试试这个:

代码语言:javascript
复制
var newimage = new Image();
newimage.src = 'retouche-hr' + newlinkimage.substring(14,17) + '-a.jpg'; 
newimage.onload = function()
{
    var width = this.naturalWidth;
    alert(width);
}
票数 50
EN

Stack Overflow用户

发布于 2009-10-30 08:33:53

这是最终的工作代码..。以防有人想知道。这一切都是等待图像加载完成的问题!

代码语言:javascript
复制
<script type="text/javascript">
    $(function() {
        $("#thumb").jCarouselLite({
            btnNext: "#down",
            btnPrev: "#up",
            vertical: true,
            visible: 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});});        
            }  
        })
    });
</script>
票数 1
EN

Stack Overflow用户

发布于 2014-01-21 22:04:23

对我来说,下面的工作...

代码语言:javascript
复制
    $('<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;
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1645166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档