Home >Web Front-end >JS Tutorial >How to use JS to solve the problem that ie6 does not support max-width and max-height
This article mainly introduces how to use JS to solve the problem that ie6 does not support max-width and max-height. Friends in need can come here for reference. I hope it will be helpful to everyone.
Today I encountered a problem about using js to solve the problem of ie6 not supporting max-width and max-height. I just started using jQuery to implement it, but I couldn’t get the value in css, such as:
if($.browser.msie && $.browser.version == 6.0) { var maxWidth = parseInt($('.viewBigPic img').css('max-width')); $('.viewBigPic img').each(function(){ if ($(this).width() > maxWidth) $(this).width(maxWidth); }); }
I don’t know why, but I can’t get the maximum value in css, so I can only use native js to achieve it
The js code is as follows:
The html code of the<script type='text/javascript'> function setPhotoSize(elem, width, height) { <!--[if IE 6]> try{ var image=new Image(); image.src=elem.src; if(image.width>0 && image.height>0){ var rate = (width/image.width < height/image.height)? width/image.width : height/image.height; if(rate <= 1){ elem.width = image.width*rate; elem.height = image.height*rate; } else { elem.width = image.width; elem.height = image.height; } } }catch(e){} <!--[endif]--> } </script>
part is as follows:
<p class="viewBigBox"> <p class="viewBigPic"> <p><img id="imgid" onload="setPhotoSize(this,730,470)" src="images/viewShow.jpg" alt=""/></p> </p> </p>
css style is as follows:
.viewBigBox{ border:1px solid #e3e3e3; background-color:#ffffff; padding:1px; margin-top:18px;} .viewBigPic{ background-color:#f7f7f7;padding:20px 14px;} .viewBigPic p{display:table-cell;width:730px; line-height:470px; overflow:hidden; vertical-align:middle; text-align:center; height:470px;*font-size:390px;} //实现图片垂直居中,主要运用了font-size与height的比例 .viewBigPic p img{ vertical-align:middle; max-height:470px; max-width:730px;}
above That’s all the content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!