Js checks whether the device is PC and adjusts the image size

Source: Internet
Author: User

Js checks whether the device is PC and adjusts the image size

This article mainly introduces js to determine whether the device is PC and adjust the image size. For more information, see

The Code is as follows:

<Html>

<Head>

<Script type = "text/javascript">

/* Determine whether the device is a PC */

Function isPC (){

Var userAgentInfo = navigator. userAgent;

Var Agents = new Array ("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod ");

Var flag = true;

For (var v = 0; v <Agents. length; v ++ ){

If (userAgentInfo. indexOf (Agents [v])> 0 ){

Flag = false;

Break;

}

}

Return flag;

}

 

/* Resize the image */

Function AutoResizeImage (maxWidth, maxHeight, objImg ){

Var img = new Image ();

Img. src = objImg. src;

Var hRatio;

Var wRatio;

Var Ratio = 1;

Var w = img. width;

Var h = img. height;

WRatio = maxWidth/w;

HRatio = maxHeight/h;

If (maxWidth = 0 & maxHeight = 0 ){

Ratio = 1;

} Else if (maxWidth = 0 ){//

If (hRatio <1) Ratio = hRatio;

} Else if (maxHeight = 0 ){

If (wRatio <1) Ratio = wRatio;

} Else if (wRatio <1 | hRatio <1 ){

Ratio = (wRatio <= hRatio? WRatio: hRatio );

}

If (Ratio <1 ){

W = w * Ratio;

H = h * Ratio;

}

ObjImg. height = h;

ObjImg. width = w;

}

 

/* Set scaling policies for different devices */

Function setImg (tagid, pcWidth, pcHeight, appWidth, appHeight ){

Var tag = document. getElementById (tagid );

Var images = tag. getElementsByTagName ("img ");

For (var I = 0; I <images. length; I ++ ){

If (isPC ){

AutoResizeImage (pcWidth, pcHeight, images [I]);

} Else {

AutoResizeImage (appWidth, appHeight, images [I]);

}

}

}

Window. onload = function (){

SetImg ('imgdiv ', 0 );

}

</Script>

</Head>

<Body>

<Div id = "imgDIV">

<Div>

</Div>

</Div>

<Br>

</Body>

</Html>

 

Related Article

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.