指定区域的图片自动按比例缩小的js代码(防止页面被图片撑破)
时间:2024-04-17 10:05:21
<div id=article><img height="800" alt="" width="1280" src="/down/js/images/12498880470.jpg" /></div>
<script type="text/javascript" >
//缩放图片到合适大小
function ResizeImages()
{
var myimg,oldwidth,oldheight;
var maxwidth=550;
var maxheight=880
var imgs = document.getElementById('article').getElementsByTagName('img'); //如果你定义的id不是article,请修改此处
for(i=0;i<imgs.length;i++){
myimg = imgs[i];
if(myimg.width > myimg.height)
{
if(myimg.width > maxwidth)
{
oldwidth = myimg.width;
myimg.height = myimg.height * (maxwidth/oldwidth);
myimg.width = maxwidth;
}
}else{
if(myimg.height > maxheight)
{
oldheight = myimg.height;
myimg.width = myimg.width * (maxheight/oldheight);
myimg.height = maxheight;
}
}
}
}
//缩放图片到合适大小
ResizeImages();
</script>
意思就是控制指定区域的的图片大小,要不一些大点的广告图片也会变形。
脚本之家用的图片控制代码:
function controlImg(ele,w,h){
var c=ele.getElementsByTagName("img");
for(var i=0;i<c.length;i++){
var w0=c[i].clientWidth,h0=c[i].clientHeight;
var t1=w0/w,t2=h0/h;
if(t1>1||t2>1||w0>=600){
c[i].width=Math.floor(w0/(t1>t2?t1:t2));
c[i].height=Math.floor(h0/(t1>t2?t1:t2));
if(document.all){
c[i].outerHTML='<a href="'+c[i].src+'" target="_blank" title="在新窗口查看图片">'+c[i].outerHTML+'</a>'
}
else{
c[i].title="在新窗口打开图片";
c[i].onclick=function(e){window.open(this.src)}
}
}
}
}
ele就是指定的区域,w是最大的宽度,大于这个就会缩小。h是最大的高度。
标签:比例缩小
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php下防止单引号,双引号在接受页面转义的设置方法
2023-11-15 02:37:01
kali中python版本的切换方法
2022-03-03 17:53:45
![](https://img.aspxhome.com/file/2023/7/93887_0s.jpg)
网页设计布局原则
2010-04-20 17:18:00
Python制作一个多功能音乐播放器
2022-06-15 10:16:05
如何合理使用数据库冗余字段的方法
2024-01-18 16:52:00
Python使用回溯法子集树模板获取最长公共子序列(LCS)的方法
2021-04-14 04:55:49
![](https://img.aspxhome.com/file/2023/5/104885_0s.jpg)
pytorch 计算ConvTranspose1d输出特征大小方式
2021-08-14 10:16:16
![](https://img.aspxhome.com/file/2023/3/118083_0s.jpg)
python中Flask框架简单入门实例
2021-04-26 05:14:50
用Oracle并行查询发挥多CPU的威力
2010-07-23 12:52:00
vue项目实现图片懒加载的简单步骤
2024-05-10 14:16:06
python如何解析复杂sql,实现数据库和表的提取的实例剖析
2024-01-21 06:37:51
JavaScript定时器实现的原理分析
2023-08-23 10:24:16
![](https://img.aspxhome.com/file/2023/4/56094_0s.jpg)
Mysql查询以某"字符串"开头的查询方式
2024-01-24 21:05:19
python解析多层json操作示例
2021-06-17 22:59:41
为什么从Python 3.6开始字典有序并效率更高
2021-03-26 15:29:36
![](https://img.aspxhome.com/file/2023/1/128261_0s.jpg)
python正则过滤字母、中文、数字及特殊字符方法详解
2022-02-20 10:14:47
基于Python实现批量缩放图片(视频)尺寸
2021-03-23 12:21:22
MySQL数据库的临时文件究竟储存在哪里
2009-02-13 13:44:00
计算机程序设计并行计算概念及定义全面详解
2023-02-20 22:35:59
![](https://img.aspxhome.com/file/2023/2/114692_0s.gif)
深入解析JavaScript中的arguments对象
2024-05-09 14:50:44