jquery loading picture Adaptive fixed size div_javascript tips

Source: Internet
Author: User
How to place a picture in a fixed-size div, display the actual size when the picture is small, and automatically adapt to the div size when the picture exceeds the div size

jquery image Adaptive Size The main code of the implementation process:

The code is as follows:
Copy Code code as follows:

. divimg{
max-height:200px; max-width:200px;
Width:expression (This.width > && this.width > This.height 200:auto);
Height:expression (This.height > 200:auto);
}

Instance:

The code is as follows:
Copy Code code as follows:

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "Text/html:charset=utf-8"/>
<title> realize picture loading adaptive </title>
<script type= "Text/javascript" src= ". /jquery.min.js "> </script>
<script type= "Text/javascript" >
function ClickMe () {
var url= ". /images/1.jpg ";
$ ("#img"). attr ("src", url);
$ ("#img"). AddClass ("divimg");
}
</script>
<style type= "Text/css" >
. divclass {
border:1px solid red;
width:200px;
height:200px;
}
. divimg{
max-height:200px; max-width:200px;
Width:expression (This.width > && this.width > This.height 200:auto);
Height:expression (This.height > 200:auto);
}
</style>
<body>
<div class= "Divclass" >
&LT;IMG id= "img" > </img>
</div>
<div>
<input type= "button" id= "btn" style= "width:50px;height:30px;border:1px solid blue;" value= "btn" onclick= "ClickMe () "/>
</div>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.