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

May 16, 2016 pm 05:05 PM
ie6

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:

1

2

3

4

5

6

7

8

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<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>

Copy after login

part is as follows:

1

2

3

4

5

<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:

1

2

3

4

5

.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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

Improve Your jQuery Knowledge with the Source Viewer

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

10 Mobile Cheat Sheets for Mobile Development

See all articles