使用JS实现HTML限定图像最大尺寸并等比例显示

调用js函数,传入Jquery对象即可:

                                      function setImgAdaptContainer(imgSelector,containerSelector){
						var _img=$(imgSelector);
						var container=$(containerSelector);
						setImgAdaptContainerByObj(_img,container);
					}
					function setImgAdaptContainerByObj(imgObj,containerObj){
						var _img=imgObj;
						var container=containerObj;
						var containerHeight=container.height()*0.99;
						var containerWidth=container.width()*0.99;
						var imgHeight=0;
						var imgWidth=0;
							_img.removeAttr('height'); _img.removeAttr('width');
							imgHeight=_img.height();
							imgWidth=_img.width();
					
						if(imgHeight/imgWidth


你可能感兴趣的:(WEB前端)