<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5+jQuery图片上传压缩预览代码</title>
</head>
<style>
*{
padding: 0;
margin: 0;
}
ul{
width: 1200px;
margin: 30px auto;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
}
ul li{
background: #00a2d4;
list-style: none;
flex-grow:10;
width: 300px;
height: 300px;
margin-right: 20px;
margin-bottom: 20px;
border: 1px solid #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
background-repeat: no-repeat;
background-size: cover;
box-sizing: border-box;
}
</style>
<script src="js/jquery.min.js"></script>
<body>
<!--上传控件-->
<input type="file" style="display: none">
<ul>
<li>上传图片</li>
</ul>
<script>
(function ($) {
$.extend({
//压缩图片,参数1:file对象,参数2:压缩比例
compress(file,scale) {
return new Promise(function (resolve,reject) {
let _scale=scale || 1;
let cvs = document.createElement('canvas');
let ctx = cvs.getContext('2d');
let img = new window.Image();
let imgType=file.type;
img.src = URL.createObjectURL(file);
img.onload=function () {
cvs.width = img.width*_scale;
cvs.height = img.height*_scale;
ctx.drawImage(img, 0, 0, cvs.width, cvs.height);
resolve(cvs.toDataURL(imgType));
}
});
}
});
$.fn.extend({
//复制节点
cloneNode(num){
let p=this.parent();
for (let i=0;i<num;i++){
p.append(this.clone(true))
}
}
});
$(function () {
$('li').cloneNode(6);//复制66个节点
//点击触发input
$('li').each(function (i) {
$(this).click(function () {
$('input').attr("name",'input_'+i).click();
})
});
$("input").change(function () {
let files=$(this)[0].files[0];//获取files对象
let index=parseInt(($(this).attr('name')).split("_")[1]);
//0.5为当前压缩比
$.compress(files,0.5).then((url)=>{
$('li').eq(index).css({"background-image": "url("+url+")"});
//上传到服务器
$.post('url',{base64:url},()=>{
})
})
})
})
})(jQuery)
</script>
<div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
<p>适用浏览器:360、FireFox、Chrome、Opera、傲游、搜狗、世界之窗. 不支持Safari、IE8及以下浏览器。</p>
<p>来源:<a href="http://www.96flw.com/" target="_blank">www.96flw.com</a></p>
</div>
</body>
</html>
HTML5+jQuery图片上传压缩预览代码.zip
版权申诉
82 浏览量
2023-09-25
13:42:37
上传
评论
收藏 34KB ZIP 举报
码云笔记
- 粉丝: 2w+
- 资源: 5837
最新资源
- 基于matlab实现图像处理,本程序使用背景差分法对来往车辆进行检测和跟踪.rar
- 基于matlab实现视频监控中车型识别代码,自己写的,希望和大家多多交流.rar
- sdk.config
- 基于matlab实现配电网三相潮流计算方法,对几种常用的配电网潮流计算方法进行了对比分析.rar
- 基于matlab实现配电网潮流 经典33节点 前推回代法潮流计算 回代电流 前推电压 带注释.rar
- 基于matlab实现模拟退火遗传算法的车辆调度问题研究,用MATLAB语言加以实现.rar
- 基于matlab实现蒙特卡洛的的移动传感器节点定位算法仿真代码.rar
- 华中数控系统818用户说明书
- 基于matlab实现卡尔曼滤波器完成多传感器数据融合 对多个机器人的不同传感器数据进行融合估计足球精确位置.rar
- 基于matlab实现进行简单车辆识别-车辆检测.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈