在html-pages上显示后,如何获得大图像的宽度和高度?

时间:2014-09-17 18:35:15

标签: javascript jquery html css

我有一个大图像,例如10000x8000px。在这种情况下,我使用CSS类 .myClass {max-width:90vw;来减少图像的宽度和高度。最大高度:80vh; :在html-page上显示后,如何获得此图像的宽度和高度。例如,如果我有1200x800显示器,我想使用 $(img).width() $(img).height()获得结果960x640px。可能吗?当我使用 $(img).width() $(img).height()时,获得自然大小10000x8000px。并且 img.clientWidth 返回0。

1 个答案:

答案 0 :(得分:1)

我的理解是,如果图像太大,您希望减小图像的大小。假设div.image是下面代码中的图像并检查代码。使用此代码可以获得所需的结果。



var width = $(".image").width();
var height = $(".image").height();

if(width > 900) {
  
	var newWidth = (width * 80) / 100; // reduce to 80% width
  $(".image").css("width", newWidth);
  
  
}

if(height > 900) {
	var newHeight = (height * 80) / 100; // reduce to 80% width
  	$(".image").css("height", newHeight);
}

.image{
  width: 1000px;
  height: 1000px;
  background-color: blue;
  display:block;
  }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="image">
</div>
&#13;
&#13;
&#13;