
JavaScript实现网页图片等比例缩放
发布时间:2006-07-29 09:25:08 来源:天极网 网友评论 0 条参考了一些代码,自己写了个图片缩放的脚本,可以点击放大,同时用鼠标滑轮自由缩放.
//图片按比例缩放,可输入参数设定初始大小
function resizeimg(ImgD,iwidth,iheight) {
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
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;
}
ImgD.style.cursor= "pointer"; //改变鼠标指针
ImgD.onclick = function() { window.open(this.src);} //点击打开大图片
if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判断浏览器,如果是IE
ImgD.title = "请使用鼠标滚轮缩放图片,点击图片可在新窗口打开";
ImgD.onmousewheel = function img_zoom() //滚轮缩放
{
var zoom = parseInt(this.style.zoom, 10) || 100;
zoom += event.wheelDelta / 12;
if (zoom> 0) this.style.zoom = zoom + "%";
return false;
}
} else { //如果不是IE
ImgD.title = "点击图片可在新窗口打开";
}
}
}
function resizeimg(ImgD,iwidth,iheight) {
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
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;
}
ImgD.style.cursor= "pointer"; //改变鼠标指针
ImgD.onclick = function() { window.open(this.src);} //点击打开大图片
if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判断浏览器,如果是IE
ImgD.title = "请使用鼠标滚轮缩放图片,点击图片可在新窗口打开";
ImgD.onmousewheel = function img_zoom() //滚轮缩放
{
var zoom = parseInt(this.style.zoom, 10) || 100;
zoom += event.wheelDelta / 12;
if (zoom> 0) this.style.zoom = zoom + "%";
return false;
}
} else { //如果不是IE
ImgD.title = "点击图片可在新窗口打开";
}
}
}
具体实现代码如下:
<img name="" src="" onload="javascript:resizeimg(this,100,200)">
可能必较多,如果有更好的代码,欢迎指教!
推荐阅讯
- JavaScript高级程序设计:2.6 原始类型
- 控制电脑关机或者重新启动的JS代码
- JavaScript高级程序设计:2.5 原始值
- 实现Title动态效果JS脚本
- 网页特效:一个简单实用的Ajax通用类
- 分析一下网页子窗口和网页父窗口的交互
- 特效:页面内容在设定时间后消失
- 如何用javascript控制上传文件的大小
- Javscript实现表单复选框的全选功能
- 减轻JavaScript测试和调试负担
阅读排行
- 1.网页特效:一段实用的图片滚动显示代码
- 2.网页特效实例:用js获取单选按钮的数据
- 3.Javascript特效:随机显示图片的源代码
- 4.Javascript技术技巧大全
- 5.IE和FireFox下javascript读写XML实现广告轮
- 6.用 JavaScript 来操作字符串的函数
- 7.添加到收藏夹的Javascript脚本-适合IE和Fir
- 8.怎样判断浏览器是否支持javascript?
- 9.JavaScript实现网页图片等比例缩放
- 10.网页特效:随机显示广告的JS源代码
专题教程
- 大话G游 专题:手机病毒揭密
- ARP攻击防范与解决方案 路由故障处理手册
- Picasa中文版_Picasa教程 专题:清除流氓软件
- Firefox专题 seo搜索引擎优化专区
- 重装Windows必知的事情 装机之必备软件大行动
病毒专杀栏
