JQuery loads an image adaptive and fixed-size DIV

Source: Internet
Author: User

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>

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.