资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

jquery实现自定义图片裁剪功能【推荐】

1.自定义宽高效果

为东阳等地区用户提供了全套网页设计制作服务,及东阳网站建设行业解决方案。主营业务为成都网站设计、成都网站建设、东阳网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

jquery实现自定义图片裁剪功能【推荐】

1.html 代码  index.html




 
 Title
 





  

2.添加插件代码

(function ($) {
 $.fn.photoCrop=function (option) {
 var opt={
 img:'',
 fixedScale:9/5,
 isHead:null,
 maxWidth:'1400',
 maxHeight:'800',
 callBack:function () {}
 }
 opt=$.extend(opt,option);
 var _this=this;
 var imgSrc=opt.img ? opt.img:_this.attr('src');
 var photoCropBox=$('
' + '
' + '
' + '
裁剪手动裁剪取消
' + '
'); $('body').append(photoCropBox); var _box=$('#photoCropBox-img'); var imgWidth=_box.find('img').width(); $('#photoCropBox-option span').css({ lineHeight:'30px', background:'#000', color:'#fff', display:'inline-block', paddingLeft:'20px', paddingRight:'20px', marginRight:'5px', cursor:'pointer' }) var cropBox=$('
' + '
' + '' + '
' + '
' + '
' + '
' + '
'); var screen=$('
') _box.append(cropBox); _box.append(screen); var _corp=$('#photoCropBox-cropBox'); var cropBoxLine=$('#cropBoxLine'); setTimeout(function () { console.log(imgWidth) cropBoxLine.find('img').css('width',_box.find('img').width()+'px') },20) if(opt.isHead){ cropBoxLine.css({borderRadius:'100%'}) } $('#photoCropBox-cropBox .line,#photoCropBox-cropBox .line2').css({ background:'url(./img/Jcrop.gif)', position:'absolute', opacity:.5 }) $('#photoCropBox-cropBox .bot').css({ background:'rgba(0,0,0,0.5)', position:'absolute', width:7, height:7, border:'1px #999 solid' }) setTimeout(function () { init(); },10) $(window).on('resize',function () { setPosition(); }) $('#photoCropBox-cancel').on('click',function () { closeBox(); }) $('#photoCropBox-bg').on('mousedown',function (e) { if(opt.fixedScale) return //固定 $('#cropBoxLine2').hide(); var _this=$(this); var _sx=e.pageX,_sy=e.pageY; var _tx=_this.offset().left; var _ty=_this.offset().top; $(document).on('mousemove',function (e) { e.preventDefault(); var _ex=e.pageX,_ey=e.pageY; getPosition(_ex,_ey,_ty,_tx,_sx,_sy,_this) }) $(document).on('mouseup',function () { $(document).unbind('mousemove'); $('#cropBoxLine2').show(); }) }) var lock=false; _corp.on('mousedown',function (e) { if(lock){return} var _sx=e.pageX,_sy=e.pageY; var pW=$('#photoCropBox-bg').width(),pH=$('#photoCropBox-bg').height(); var _this=$(this),_thisX=parseInt(_this.css('left')),_thisY=parseInt(_this.css('top')),_thisW=parseInt(_this.css('width')),_thisH=parseInt(_this.css('height')); $(document).on('mousemove',function (e) { e.preventDefault(); var _ex=e.pageX,_ey=e.pageY; var _x=_ex-_sx,_y=_ey-_sy; _x+=_thisX;_y+=_thisY; if(_x<0) _x=0; if(_y<0) _y=0; if(_y>pH-_thisH) _y=pH-_thisH; if(_x>pW-_thisW) _x=pW-_thisW; resizeCropBox("","",_y,_x,true) }) $(document).on('mouseup',function () { $(document).unbind('mousemove'); }) }) //控制大小 $('#cropBoxLine2 .bot').on("mousedown",function (e) { lock=true; var _esx=e.pageX,_esy=e.pageY; var _that=$(this); var _this=$('#photoCropBox-bg'); var _tx=_this.offset().left; var _ty=_this.offset().top; var _sx=_corp.offset().left,_sy=_corp.offset().top;//裁剪框 if(_that.hasClass('right-top')) _sy+=_corp.height(); if(_that.hasClass('left-top')){ _sy+=_corp.height(); _sx+=_corp.width(); } if(_that.hasClass('left-bottom')) _sx+=_corp.width(); $(document).on('mousemove',function (e) { e.preventDefault(); var _ex=e.pageX,_ey=e.pageY; if(opt.fixedScale){ _ey=(_ex-_esx)/opt.fixedScale+_esy; if(_that.hasClass('right-top') || _that.hasClass('left-bottom')){ _ey=(_esx-_ex)/opt.fixedScale+_esy; } } getPosition(_ex,_ey,_ty,_tx,_sx,_sy,_this) }) $(document).on('mouseup',function () { $(document).unbind('mousemove'); lock=false; }) }) $('#cropBoxLine2 .left,#cropBoxLine2 .top,#cropBoxLine2 .right,#cropBoxLine2 .bottom').on('mousedown',function (e) { if(opt.fixedScale) return //固定 lock=true; var _that=$(this); var _this=$('#photoCropBox-bg'); var _tx=_this.offset().left;// var _ty=_this.offset().top; var _sx=_corp.offset().left,_sy=_corp.offset().top; var ch=_corp.height(),cw=_corp.width(); if(_that.hasClass('top')){ _sy+=ch; }else if(_that.hasClass('left')) { _sx+=cw; } $(document).on('mousemove',function (e) { e.preventDefault(); var _ex=e.pageX,_ey=e.pageY; if(_that.hasClass('top') || _that.hasClass('bottom')){ if(!(_ey-_sy>0)){ var _x=_sx-_tx,_y=_ey-_ty,_w=cw,_h=-(_ey-_sy); if(_y<0) {_y=0;_h=_sy-_ty;} }else{ var _x=_sx-_tx,_y=_sy-_ty,_w=cw,_h=_ey-_sy; if(_h>_this.height()-_y) _h=_this.height()-_y; } }else { if(_ex-_sx>0 && _ey-_sy>0){ var _x=_sx-_tx,_y=_sy-_ty,_w=_ex-_sx,_h=ch; if(_w>_this.width()-_x) _w=_this.width()-_x; }else if(!(_ex-_sx>0) && _ey-_sy>0){ var _x=_ex-_tx,_y=_sy-_ty,_w=-(_ex-_sx),_h=ch; if(_x<0) {_x=0;_w=_sx-_tx;} } } resizeCropBox(_w,_h,_y,_x); }) $(document).on('mouseup',function () { $(document).unbind('mousemove'); lock=false; }) }) $('#photoCropBox-start').on('click',function () { _corp.css('display','block') $('#photoCropBox-bg').css('display','block') }) $('#photoCropBox-end').on('click',function () { getImage() closeBox() }) function init() { setPosition() if(opt.fixedScale){ if((_box.height()-_box.width()/opt.fixedScale/2)<0){ resizeCropBox(_box.height()*opt.fixedScale,_box.height(),0,(_box.width()-_box.height()*opt.fixedScale)/2) }else { resizeCropBox(_box.width()/2,_box.width()/opt.fixedScale/2,(_box.height()-_box.width()/opt.fixedScale/2)/2,_box.width()/4) } }else { resizeCropBox(_box.width()/2,_box.height()/2,_box.height()/4,_box.width()/4) } if(opt.fixedScale) { $('.bot.top,.bot.left,.bot.bottom,.bot.right').remove();//固定 } } function setPosition() { $('#photoCropBox-panel').css({ top:($('#photoCropBox-panel-box').height()-$('#photoCropBox-panel').height())/2+'px', left:($('#photoCropBox-panel-box').width()-$('#photoCropBox-panel').width())/2+'px', opacity:1 }) } //结束x,y 背景x,y function getPosition(_ex,_ey,_ty,_tx,_sx,_sy,_this) { if(_ex-_sx>0 && _ey-_sy>0){ var _x=_sx-_tx,_y=_sy-_ty,_w=_ex-_sx,_h=_ey-_sy; if(_w>_this.width()-_x) _w=_this.width()-_x; if(_h>_this.height()-_y) _h=_this.height()-_y; }else if(!(_ex-_sx>0) && _ey-_sy>0){ var _x=_ex-_tx,_y=_sy-_ty,_w=-(_ex-_sx),_h=_ey-_sy; if(_x<0) {_x=0;_w=_sx-_tx;} if(_h>_this.height()-_y) _h=_this.height()-_y; }else if(!(_ex-_sx>0) && !(_ey-_sy>0)){ var _x=_ex-_tx,_y=_ey-_ty,_w=-(_ex-_sx),_h=-(_ey-_sy); if(_x<0) {_x=0;_w=_sx-_tx;} if(_y<0) {_y=0;_h=_sy-_ty;} }else if(_ex-_sx>0 && !(_ey-_sy>0)){ var _x=_sx-_tx,_y=_ey-_ty,_w=_ex-_sx,_h=-(_ey-_sy); if(_y<0) {_y=0;_h=_sy-_ty;} if(_w>_this.width()-_x) _w=_this.width()-_x; } if(opt.fixedScale){ if(_w/opt.fixedScale>_h){ _w=_h*opt.fixedScale }else if (_w

3.绑定代码

$(function () {
 $('.source').on('click',function () {
 $(this).photoCrop({
 fixedScale:false,
 isHead:false,
 callBack:function(url){
 $('.target').attr('src',url)
 },
 });
 })
 
 })

2.宽高比例固定效果

jquery实现自定义图片裁剪功能【推荐】

代码:

$(function () {
 $('.source').on('click',function () {
 $(this).photoCrop({
 fixedScale:5/6,
 isHead:false,
 callBack:function(url){
 $('.target').attr('src',url)
 },
 });
 })
 })

3.头像裁剪效果

jquery实现自定义图片裁剪功能【推荐】

代码:

$(function () {
 $('.source').on('click',function () {
 $(this).photoCrop({
 fixedScale:1,
 isHead:true,
 callBack:function(url){
 $('.target').attr('src',url)
 },
 });
 })
 })

以上代码易修改,大家可以自行扩展。希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持创新互联!


网页标题:jquery实现自定义图片裁剪功能【推荐】
文章链接:http://cdkjz.cn/article/pogdep.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220