[JavaScript] JavaScript根据需要动态缩放网页上的图片以适应屏幕大小 →→→→→进入此内容的聊天室

来自 , 2019-02-26, 写在 JavaScript, 查看 149 次.
URL http://www.code666.cn/view/82356b37
  1. <html>
  2. <head>
  3. <title>JavaScript等比例缩放图片</title>
  4. <script>
  5. function DrawImage(ImgD,iwidth,iheight){ //参数(图片,允许的宽度,允许的高度)
  6. var image=new Image();
  7. image.src=ImgD.src;
  8. if(image.width>0
  9. && image.height>0){ if(image.width/image.height >= iwidth / iheight)
  10. {
  11.     if (image.width > iwidth) {
  12.         ImgD.width = iwidth;
  13.         ImgD.height = (image.height * iwidth) / image.width;
  14.     }
  15.     else {
  16.         ImgD.width = image.width;
  17.         ImgD.height = image.height;
  18.     }
  19. }
  20. else
  21. {
  22.     if (image.height > iheight) {
  23.         ImgD.height = iheight;
  24.         ImgD.width = (image.width * iheight) / image.height;
  25.     }
  26.     else {
  27.         ImgD.width = image.width;
  28.         ImgD.height = image.height;
  29.     }
  30. }
  31. }
  32. }
  33.  </script>
  34. </head>
  35. < body>
  36. <img src="http://www.haotu.net/up/2013/08/02/137540300905687.jpg"  alt="图片自动缩放" width = "100" height = "100" onload="javascript:DrawImage(this,80,80)" />
  37. </body></html>
  38. //javascript/8315

回复 "JavaScript根据需要动态缩放网页上的图片以适应屏幕大小"

这儿你可以回复上面这条便签

captcha