简体   繁体   English

我想在javascript中动态调整图像怎么做?

[英]I want dynamic adjust a image in javascript how to do it?

my picture create by user and by js 我的图片由用户和js创建

var imgpre=document.createElement("img");

I want show in a box or div or td but sometime the img is so large. 我希望在一个盒子或divtd中显示,但有时img是如此之大。 I mean I don't know the image height and width, but I want put the image to 350*350 box. 我的意思是我不知道图像的高度和宽度,但我想把图像放到350 * 350的盒子里。 The image could not draw outside the box the problem is sometime the image is 400*200, I set the img.height=350 , it should ok to look at. 图像无法在框外绘制问题是有时图像是400 * 200,我设置了img.height=350 ,它应该可以查看。 But sometimes the picture is 200*400, when you set img.height=350 where is definitely wrong. 但有时图片是200 * 400,当你设置img.height=350肯定是错的。

I could not get img.height in js, I tried but it return zero before I set it. 我无法在js中获得img.height ,我尝试但在设置之前它返回零。

please note: I do not want tensile image. 请注意:我不想要拉伸图像。

I got correct answer: 我得到了正确答案:

imgpre.setAttribute("style", "max-width:100%; max-height:100%;"); imgpre.setAttribute(“style”,“max-width:100%; max-height:100%;”);

and also set the mzx-width:350 and max-height: 350 the box 并设置mzx-width:350和max-height:350框

Try 尝试

imgpre.height = '100px';
imgpre.width = '100px';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM