js判断设备是否为PC并调整图片大小
时间:2024-05-02 16:12:22
<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="http://192.168.1.116:9999/ffzx/news/20140205/015212022_1.jpg" />
<div>
<img alt="" src="http://192.168.1.116:9999/ffzx/news/20140208/1386835169183.jpg" />
</div>
</div>
<br>
</body>
</html>
标签:不同设备,图片大小
0
投稿
猜你喜欢
配置SQL Server 2000选项
2010-04-25 11:01:00
python嵌套函数使用外部函数变量的方法(Python2和Python3)
2022-05-16 20:29:25
Python 时间操作time详情
2023-09-13 13:23:09
asp如何用Access加密页面?
2010-06-10 18:41:00
Sql Server基本函数
2024-01-24 05:32:29
vue2.0开发实践总结之疑难篇
2024-05-02 16:33:28
URL编码“陷阱”
2008-03-04 16:57:00
很全面的MySQL处理重复数据代码
2024-01-24 14:26:45
Golang拾遗之实现一个不可复制类型详解
2024-05-13 10:40:29
浅谈SQL与PLSQL开发实战
2011-05-05 08:15:00
详解git merge 与 git rebase的区别
2023-04-07 20:02:49
怎么样才能抓住用户?
2008-10-20 12:10:00
Python运算符教程之逻辑门详解
2021-05-17 04:32:54
JavaScript实现日期格式化的方法汇总
2024-04-10 16:19:04
Python基于pyecharts实现关联图绘制
2021-04-08 12:05:45
python typing模块--类型提示支持
2023-11-11 16:47:27
关于《回访确认》的几个问题
2009-08-24 12:43:00
python控制windows剪贴板,向剪贴板中写入图片的实例
2021-03-27 07:35:31
一篇文章带你学习Python3的高级特性(1)
2021-09-24 04:39:01
基于Python实现有趣的象棋游戏
2022-06-11 20:37:15