How to place an image in a fixed-size div. When the image size is small, the actual size is displayed. When the image size exceeds the div size, the image automatically adapts to the div size.
The main code of jquery Image Adaptive size implementation process:
The Code is as follows:
Copy codeThe Code is as follows:
. DivImg {
Max-height: 200px; max-width: 200px;
Width: expression (this. width> 200 & this. width> this. height? 200: auto );
Height: expression (this. height> 200? 200: auto );
}
Instance:
The Code is as follows:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html: charset = UTF-8"/>
<Title> adaptive image loading </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> 200 & this. width> this. height? 200: auto );
Height: expression (this. height> 200? 200: auto );
}
</Style>
</Head>
<Body>
<Div class = "divClass">
</img>
</Div>
<Div>
<Input type = "button" id = "btn" style = "width: 50px; height: 30px; border: 1px solid blue; "value =" btn "onclick =" clickMe () "/>
</Div>
</Body>
</Html>