Home > Web Front-end > JS Tutorial > How to use JS to solve the problem that ie6 does not support max-width and max-height

How to use JS to solve the problem that ie6 does not support max-width and max-height

PHPz
Release: 2018-09-30 17:07:06
Original
1343 people have browsed it

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);
    });
  }
Copy after login

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=&#39;text/javascript&#39;>
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>
Copy after login

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>
Copy after login

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;}
Copy after login

above That’s all the content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Related labels:
ie6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template