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>