javascript实现获取图片大小及图片等比缩放的方法
本文实例讲述了javascript实现获取图片大小及图片等比缩放的方法。分享给大家供大家参考,具体如下:
获取图片大小:
varoriginImage=newImage();
functionGetImageWidth(oImage){
if(originImage.src!=oImage.src)originImage.src=oImage.src;
returnoriginImage.width;
}
functionGetImageHeight(oImage){
if(originImage.src!=oImage.src)originImage.src=oImage.src;
returnoriginImage.height;
}
图片等比缩放:
functionSetImage(ImgD,FitWidth,FitHeight){
varimage=newImage();
image.src=ImgD.src;
if(image.width>0&&image.height>0){
if(image.width/image.height>=FitWidth/FitHeight){
if(image.width>FitWidth){
ImgD.width=FitWidth;
ImgD.height=(image.height*FitWidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}else{
if(image.height>FitHeight){
ImgD.height=FitHeight;
ImgD.width=(image.width*FitHeight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript切换特效与技巧总结》、《JavaScript图形绘制技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。