【问题标题】:Getting the width from an scaled image从缩放的图像中获取宽度
【发布时间】:2012-03-23 12:35:18
【问题描述】:

一旦图像附加到正文,我正在使用 Jquery 获取图像的宽度。查看this fiddle 以了解我在做什么。

我想要做的是添加每个元素的高度受限,并在缩放后读取相应的宽度。您将看到的问题是 console.log 给我的宽度值为 0。

【问题讨论】:

    标签: javascript jquery width image


    【解决方案1】:

    莫森和正义是对的。但是,您仍然可以简化一点:

    var number = 0;
    $(document).ready(function(){
        setInterval(function(){
            console.log();
            $('.container').prepend($('<img '
                    + 'id="img'+number+'"src="http://www.upress.umn.edu/'
                    + 'book-division/images-1/other-'
                    + 'images/blockc.gif/image_thumb">').load(function() {
                console.log($(this).width());            
            }));
            number++;
        }, 3000);
    });​
    

    【讨论】:

      【解决方案2】:

      使用jQuery.load()函数完成图片加载后的所有计算:

      变量编号 = 0;

      $(document).ready(function(){
          setInterval(function(){
              console.log();
              $('.container').prepend(
                $('<img/>').attr({
                 src: "http://www.upress.umn.edu/book-division/images-1/other images/blockc.gif/image_thumb"
                 id:'img' + (number++)
      }).load(function(){
       console.log($(this).width())
      })
          },3000);
      });​
      

      【讨论】:

        【解决方案3】:

        http://jsfiddle.net/w7rcn/

        在图像完全下载之前“读取”宽度。

        所以,我们使用了相同的概念,但在 img 加载事件触发后应用它:

         $('#img' + String(number)).load(function() {
                    console.log($(this).width());
                });
        

        【讨论】:

          猜你喜欢
          • 2012-08-04
          • 1970-01-01
          • 2022-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多