30 javascript ajax
我需要使用AJAX在网页上显示一堆图像.所有这些都有不同的尺寸,所以我想在显示它们之前调整它们的尺寸.有没有办法在JavaScript中这样做?
getimagesize()对每个图像使用PHP 会导致不必要的性能损失,因为会有很多图像.
小智 77
我正在寻找一种解决方案,使用JavaScript获取图像的高度和宽度.我找到了很多,但所有这些解决方案仅在图像存在于浏览器缓存中时才有效.
最后,我找到了一个解决方案来获取图像的高度和宽度,即使图像在浏览器缓存中不存在:
<script type="text/javascript">
var imgHeight;
var imgWidth;
function findHHandWW() {
imgHeight = this.height;
imgWidth = this.width;
return true;
}
function showImage(imgPath) {
var myImage = new Image();
myImage.name = imgPath;
myImage.onload = findHHandWW;
myImage.src = imgPath;
}
</script>
Run Code Online (Sandbox Code Playgroud)
谢谢,
Binod Suman
http://binodsuman.blogspot.com/2009/06/how-to-get-height-and-widht-of-image.html
Ste*_*ton 21
试试这个:
var curHeight;
var curWidth;
function getImgSize(imgSrc)
{
var newImg = new Image();
newImg.src = imgSrc;
curHeight = newImg.height;
curWidth = newImg.width;
}
Run Code Online (Sandbox Code Playgroud)