JS自动缩小超出大小的图片
2023-06-13 09:14:40 时间
<scripttype="text/javascript"src="js/jquery.min.js"></script>
<scripttype="text/javascript">
jQuery.fn.LoadImage=function(scaling,width,height,loadpic){
if(loadpic==null)loadpic="../img/loading.gif";
returnthis.each(function(){
vart=$(this);
varsrc=$(this).attr("src")
varimg=newImage();
img.src=src;
//自动缩放图片
varautoScaling=function(){
if(scaling){
if(img.width>0&&img.height>0){
if(img.width/img.height>=width/height){
if(img.width>width){
t.width(width);
t.height((img.height*width)/img.width);
}else{
t.width(img.width);
t.height(img.height);
}
}
else{
if(img.height>height){
t.height(height);
t.width((img.width*height)/img.height);
}else{
t.width(img.width);
t.height(img.height);
}
}
}
}
}
//处理ff下会自动读取缓存图片
if(img.complete){
autoScaling();
return;
}
$(this).attr("src","");
varloading=$("<imgalt=\"加载中...\"title=\"图片加载中...\"src=\""+loadpic+"\"/>");
t.hide();
t.after(loading);
$(img).load(function(){
autoScaling();
loading.remove();
t.attr("src",this.src);
t.show();
});
});
}
</script>
<divid="content"><imgsrc="img/20120518073933709.jpg"/></div>
<scripttype="text/javascript">
<!--
$(window).load(function(){
$("#contentimg").LoadImage(true,600,500,"img/loading.gif");
});
//-->
</script>
相关文章
- js控制30秒自动页面跳转,带读秒
- html如何只刷新页面指定,js控制页面刷新 JS刷新当前页面的几种方法总结
- Node.js + GitHub Actions 自动刷新 CDN
- JS跳转代码_js中跳转页面路径
- 原生js发送post请求_javascript发送post请求
- 使用 Node.js + OPEN AI 实现一个自动生成图片项目
- 【JS 逆向百例】猿人学系列 web 比赛第二题:js 混淆 - 动态 cookie,详细剖析
- 【源码】PDF.js批注注释插件库(纯JS). 创建和保存PDF批注(PDF 高亮/签名/插图/截屏/文本框/画笔/多边形)-pdf.js
- 让文章自动生成章节目录索引的JS代码详解编程语言
- 利用 JS 实现 Redis 的连接(js连接redis)
- jsdefineSetter-给js的"class"自动增加一个set的属性(方法)
- JS页面自动加载函数(兼容多浏览器)
- js变量类型转换常用函数与代码[比较全]
- 输入数量和价格自动计算输出金额的js代码
- 当鼠标滑过文本框自动选中输入框内容的JS代码分享
- js弹出确认是否删除对话框
- js写出遮罩层登陆框和对联广告并自动跟随滚动条滚动
- js图片自动轮播代码分享(js图片轮播)
- js表头排序实现方法
- JS实现页面超时后自动跳转到登陆页面
- Ajax中通过JS代码自动获取表单元素值的示例代码