网页中图片大小自动调整三种方法
1.鼠标滚动图片大小调整
<body>
<script LANGUAGE="JAVASCRIPT">
function bbimg(o){
var zoom=parseInt(o.style.zoom, 10)||100;
zoom+=event.wheelDelta/12;
if (zoom>0)
o.style.zoom=zoom+'%';
return false;
}
</script>
<img src="123.jpg" onload="javascript:if(this.width>screen.width-screen.width/2) this.style.width=screen.width-screen.width/2" onmousewheel="return bbimg(this)" >
</body>
2.图片自动缩小到固定大小
<body onload="DrawImage(theimg)">
<script language="JavaScript">
<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD){
var image=new Image();
var iwidth = 320; //定义允许图片宽度
var iheight = 180; //定义允许图片高度
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>
<img src="123.jpg" width="1024" height="768" name="theimg">
</body>
3.滚轮图片缩放的代码
<script language="javascript">
var count = 10;
function resizeimg(oImage)
{
count = Counting(count);
Resize(oImage,count);
return false;
}
function Counting(newzoom){
if (event.wheelDelta >= 120)
newzoom++;
else if (event.wheelDelta <= -120)
newzoom--;
if (newzoom<2) newzoom=2; ////只允许缩小到20%
if (newzoom>50) newzoom=50; ////只允许放大到500%
return newzoom;
}
function Resize(oImage,newzoom){
oImage.style.zoom = newzoom + '0%';
count=newzoom;
}
</script>
然后在<img src="">中加入
onDblClick="return Resize(this,10);return false;"
onmousewheel="return resizeimg(this)"
<body>
<script LANGUAGE="JAVASCRIPT">
function bbimg(o){
var zoom=parseInt(o.style.zoom, 10)||100;
zoom+=event.wheelDelta/12;
if (zoom>0)
o.style.zoom=zoom+'%';
return false;
}
</script>
<img src="123.jpg" onload="javascript:if(this.width>screen.width-screen.width/2) this.style.width=screen.width-screen.width/2" onmousewheel="return bbimg(this)" >
</body>
2.图片自动缩小到固定大小
<body onload="DrawImage(theimg)">
<script language="JavaScript">
<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD){
var image=new Image();
var iwidth = 320; //定义允许图片宽度
var iheight = 180; //定义允许图片高度
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>
<img src="123.jpg" width="1024" height="768" name="theimg">
</body>
3.滚轮图片缩放的代码
<script language="javascript">
var count = 10;
function resizeimg(oImage)
{
count = Counting(count);
Resize(oImage,count);
return false;
}
function Counting(newzoom){
if (event.wheelDelta >= 120)
newzoom++;
else if (event.wheelDelta <= -120)
newzoom--;
if (newzoom<2) newzoom=2; ////只允许缩小到20%
if (newzoom>50) newzoom=50; ////只允许放大到500%
return newzoom;
}
function Resize(oImage,newzoom){
oImage.style.zoom = newzoom + '0%';
count=newzoom;
}
</script>
然后在<img src="">中加入
onDblClick="return Resize(this,10);return false;"
onmousewheel="return resizeimg(this)"
本文来自博客园,作者:古道轻风,转载请注明原文链接:https://www.cnblogs.com/88223100/archive/2008/07/12/1241590.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析