2015-06-06 93 views
0

的高度和宽度,这是工作的罚款,并给予我的图像,但这些图像是不同分辨率的,我想他们都可以200×200我将如何设置动态图像

HTML的

<div class="container-fluid" id="myElement"> <div id="images"> </div></div>

JavaScript函数

function appendReturnedImages(data) { 


    $.each(data.images, function(index, element) { 

    $("#myElement #images").append($('<img height="200" style="max-width: 200px">', { 

     src: element 

    })); 

}); 

} 
+0

这是什么问题?不要追加在循环中! – lshettyl

+0

我将如何设置动态图像的高度和宽度 –

+0

如何将图像大小调整为200x200 –

回答

0

正如我刚才所说,不要在一个循环追加,因为它的昂贵!更清晰和正确的追加方法如下。

var $html = $(); 

$.each(data.images, function(index, element) { 
    $html = $html.add($("<img/>", { 
     height: 200, 
     //width: 200, // uncomment this if you need to set width as well 
     css: { 
      'max-width': 200 
     }, 
     src: element 
    }));  
}); 
// You shouldn't be using $("#myElement #images") as an ID must be unique in a document! 
$("#images").append($html); 
+0

谢谢你,你做到了。 –

+0

我该如何与视频做同样的事情? –

+0

不确定你的意思。尝试类似的方法或张贴另一个问题。 – lshettyl

0

如果图像是不是比为1:1,你不能在分辨率200x200px而不变形表现出来。 HTML不能只显示图像的一部分,总是整体。

您需要准备缩略图200x200并使用它们,或将图像设置为背景以获得尺寸为200x200px的元素。

+0

我可以使用代码解决此问题。因为图像来自服务器。 –

+0

@AbdurRehmanFarooqi:好的,比:HTML或CSS都不能裁剪图像。只需以正确的比例显示它,或使其变形。不裁剪或仅显示一部分。 – panther

+0

我该怎么做? –

0

不应该在该行:

$("#myElement #images").append($('<img height="200" style="max-width: 200px">', { 

这个样子的?:

$("#myElement #images").append($('<img style="width: 200px; height: 200px;">', { 
+0

是的,但图像不以这种方式显示。 –

相关问题