function fiximg(wimg,maxwidth,maxheight){ var rwidth = wimg.width; console.log(wimg); var rheight = wimg.height; if(rwidth<=maxwidth&&rheight<=maxheight){ wimg.width = rwidth; wimg.height = rheight; }else { if(rwidth/rheight<maxwidth/maxheight){ wimg.height = maxheight; wimg.width = wimg.width*maxheight/wimg.height; }else{ wimg.width = maxwidth; wimg.height = wimg.height*maxwidth/wimg.width; } } } window.onload = function(){ var xx = document.getElementById('cimg'); fiximg(xx,400,400); }
JavaScript控制固定边框,图片全部展示