【问题标题】:how to define the size of image like width and height which is loaded from the server如何定义从服务器加载的图像的大小,如宽度和高度
【发布时间】:2014-05-20 05:42:44
【问题描述】:

我已成功从服务器加载图像并将其显示到 div 元素。但我想将图像调整为大。我该怎么做。这是我从服务器加载图像的代码。

var img = $("<img />").attr('src', url)
    .load(function() {
        if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
            alert('broken image!');
        } else {
            $("#image").append(img);
        }
    });

【问题讨论】:

标签: javascript jquery image


【解决方案1】:

您可以使用 width() 和 height() getter 和 setter。

var xDimension=150;
$(img).load(function(){ 
     // put your logic here
  }).width(xDimension).height(xDimension);

【讨论】:

    【解决方案2】:

    在您的 css 文件中添加如下样式:

    #image img{width: your width; height: your height;}
    

    它也适用于动态加载的图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      相关资源
      最近更新 更多