复制代码 代码如下:
<html>
<head>
<script type="text/javascript">
/* 判断设备是否为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;
}

/* 调整图片大小 */
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;
}

/* 设置不同设备的缩放策略 */
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',300,0,300,0);
}
</script>
</head>
<body>
<div id="imgDIV">
<img alt="" src="/UploadFiles/2021-04-02/015212022_1.jpg"><div>
<img alt="" src="http://192.168.1.116:9999/ffzx/news/20140208/1386835169183.jpg"></div>
</div>
<br>
</body>
</html>

标签:
不同设备,图片大小

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“js判断设备是否为PC并调整图片大小”

暂无“js判断设备是否为PC并调整图片大小”评论...