支持裁剪的jQuery上传头像图片插件

来源:https://www.sucaihuo.com/js/3037.html 2017-09-17 23:55浏览(1944) 收藏

一款支持裁剪的jQuery上传头像图片插件,点击上传按钮,会有一个弹窗要求选择图片,选中图片后会有一个裁剪的对话窗口,裁剪完成后点击保存即可看到裁减后的头像。
支持裁剪的jQuery上传头像图片插件
分类:表单代码 > 图片上传 难易:初级
查看演示 下载资源 下载积分: 20 积分
关注公众号,免费赠送安装视频教程、环境和学习视频,后面会不断更新。

页面的head部分,需引入页面所需的CSS样式文件和JS插件等,代码如下:

<link type="text/css" rel="stylesheet" href="css/ycbootstrap.css"/>
<link type="text/css" rel="stylesheet" href="css/reset.css"/>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="plugins/cover_js/iscroll-zoom.js" charset="utf-8"></script>
<script type="text/javascript" src="plugins/cover_js/hammer.js" charset="utf-8"></script>
<script type="text/javascript" src="plugins/cover_js/lrz.all.bundle.js" charset="utf-8"></script>
<script type="text/javascript" src="plugins/cover_js/jquery.photoClip.min.js" charset="utf-8"></script>

页面的body部分,需设置好图片上传和裁剪所需的容器等,代码如下:

<div class="yc-upload-wrap">
	<div class="yc-upload-box">
		<div class="container">
        	<div class="row">
		        <div class="col-md-12 col-sm-12 col-xs-12" style="padding:0;">
		        	
		        	<div class="ycupload-mainbox">
		        		<div class="ycupload-main1" style="overflow:hidden;padding-left:25px;">
		        			<span style="float:left;color:#ff5a5a;font-size:14px;line-height:60px;font-weight:900;margin-right:7px;">
		        				&middot;
		        			</span>
		        			<span style="float:left;color:#333;font-size:16px;line-height:60px;margin-right:28px;">
		        				上传封面
		        			</span>
		        		</div>
		        		<div class="ycupload-line"></div>
		        		<div style="height:30px;"></div>
		        		<div  style="min-height:1px;">
		        			<div class="container">
					        	<div class="row">
							        <div class="col-md-12 col-sm-12 col-xs-12" style="padding-right:0;padding-left:36px;">
							        	<!--<a href="javascript:void(0);" class="cover-uploadBtn">
							        		<img src="img/yc_uploadimg_06.png"/>
							        		<div id="clipArea"></div>
											<input type="file" id="file">
											<button id="clipBtn">截取</button>
							        	</a>
							        	<div id="view"></div>-->
							        	<div style="min-height:1px;line-height:160px;text-align:center;position:relative;" ontouchstart="">
							        		<div class="cover-wrap" style="display:none;position:fixed;left:0;top:0;width:100%;height:100%;background: rgba(0, 0, 0, 0.4);z-index: 10000000;text-align:center;">	
							        			<div class="" style="width:900px;height:600px;margin:100px auto;background-color:#FFFFFF;overflow: hidden;border-radius:4px;">
							        				<div id="clipArea" style="margin:10px;height: 520px;"></div>
							        				<div class="" style="height:56px;line-height:36px;text-align: center;padding-top:8px;">
							        					<button id="clipBtn" style="width:120px;height: 36px;border-radius: 4px;background-color:#ff8a00;color: #FFFFFF;font-size: 14px;text-align: center;line-height: 36px;outline: none;">保存封面</button>
							        				</div>
							        			</div>
							        		</div>
							        		<div id="view" style="width:214px;height:160.5px;" title="请上传 428*321 的封面图片"></div>
								        	<div style="height:10px;"></div>
								        	<div class="" style="width:140px;height:32px;border-radius: 4px;background-color:#ff8a00;color: #FFFFFF;font-size: 14px;text-align:center;line-height:32px;outline:none;margin-left:37px;position:relative;">
								        		点击上传封面图
								        		<input type="file" id="file" style="cursor:pointer;opacity:0;filter:alpha(opacity=0);width:100%;height:100%;position:absolute;top:0;left:0;">
								        	</div>
							        	</div>
						        	</div>
					        	</div>
				        	</div>
				        	
		        		</div>
		        		<div style="height:25px;"></div>
		        	</div>
		        	
	        	</div>
        	</div>
    	</div>
    	
	</div>
</div>

页面的底部,需对指定元素启用图像上传裁剪插件,并设置好相关参数,代码如下:

//上传封面
//document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
var clipArea = new bjj.PhotoClip("#clipArea", {
	size: [428, 321],// 截取框的宽和高组成的数组。默认值为[260,260]
	outputSize: [428, 321], // 输出图像的宽和高组成的数组。默认值为[0,0],表示输出图像原始大小
	//outputType: "jpg", // 指定输出图片的类型,可选 "jpg" 和 "png" 两种种类型,默认为 "jpg"
	file: "#file", // 上传图片的<input type="file">控件的选择器或者DOM对象
	view: "#view", // 显示截取后图像的容器的选择器或者DOM对象
	ok: "#clipBtn", // 确认截图按钮的选择器或者DOM对象
	loadStart: function() {
		// 开始加载的回调函数。this指向 fileReader 对象,并将正在加载的 file 对象作为参数传入
		$('.cover-wrap').fadeIn();
		console.log("照片读取中");
	},
	loadComplete: function() {
		 // 加载完成的回调函数。this指向图片对象,并将图片地址作为参数传入
		console.log("照片读取完成");
	},
	//loadError: function(event) {}, // 加载失败的回调函数。this指向 fileReader 对象,并将错误事件的 event 对象作为参数传入
	clipFinish: function(dataURL) {
		 // 裁剪完成的回调函数。this指向图片对象,会将裁剪出的图像数据DataURL作为参数传入
		$('.cover-wrap').fadeOut();
		$('#view').css('background-size','100% 100%');
		console.log(dataURL);
	}
});
//clipArea.destroy();
标签: 上传头像裁剪
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2