jquery實作自訂圖片裁切功能代碼分享
圖片自訂裁切如何實現?本文主要介紹了jquery實作自訂圖片裁切功能,程式碼超級簡單,容易修改。下面跟著小編一起來看吧,希望能幫助大家。
1.自訂寬高效果
#1.html 程式碼 index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="./jquery-1.12.4.min.js"></script> </head> <body> <img class="source" src="./test2.jpg" alt=""> <img src="" class="target" alt=""> </body> </html>
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=$('<p id="photoCropBox" style="position: fixed;width: 100%;height: 100%;top: 0;left: 0;background: rgba(0,0,0,0.5);z-index: 99999;padding: 20px;">' + '<canvas id="cropCanvas" style="position: absolute;opacity:1;left: 0;top: 0;z-index: 100"></canvas><img id="dataImg" src="'+imgSrc+'" style="opacity: 0;position: absolute" alt=""><p id="photoCropBox-panel-box" style="position: relative;width: 100%;height: 100%;">' + '<p id="photoCropBox-panel" style="opacity:0;background: #eee;border-radius: 5px;max-width: '+opt.maxWidth+'px;max-height: '+opt.maxHeight+'px;position: absolute;text-align: center"><p id="photoCropBox-img" style="margin: 40px 60px 20px;display: inline-block;position: relative">' + '<img src="'+imgSrc+'" style="max-width: 100%;display: block;max-height: 100%;max-height: '+(opt.maxHeight-110)+'px;" alt=""></p><p id="photoCropBox-option" style="text-align: right;padding-right: 50px;padding-bottom: 20px;position: relative;z-index: 2"><span id="photoCropBox-end">裁剪</span><span id="photoCropBox-start">手动裁剪</span><span id="photoCropBox-cancel">取消</span></p></p>' + '</p></p>'); $('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=$('<p id="photoCropBox-cropBox" style="position: absolute;z-index: 5;cursor: Move;display: none">' + '<p id="cropBoxLine" style="overflow: hidden;position: absolute;width: 100%;height: 100%;">' + '<img src="'+imgSrc+'" style="display: block;width: '+_box.find('img').width()+'px;position: absolute;max-height: none;max-width: none" alt="">' + '<p class="top line" style="width: 100%;height: 1px;top: 0;left: 0;"></p><p class="right line" style="height: 100%;width: 1px;top: 0;right: 0"></p>' + '<p class="line bottom" style="width: 100%;height: 1px;bottom: 0px;left: 0"></p><p class="left line" style="height: 100%;width: 1px;top: 0;left: 0"></p></p>' + '<p id="cropBoxLine2"><p class="left line2" style="height: 100%;width: 1px;top: 0;left: 0;cursor: w-resize"></p><p class="right line2" style="height: 100%;width: 1px;top: 0;right: 0;cursor: e-resize"></p><p class="top line2" style="width: 100%;height: 1px;top: 0;left: 0;cursor: n-resize;position: absolute"></p><p class="bottom line2" style="width: 100%;height: 1px;bottom: 0px;left: 0;cursor: s-resize"></p>' + '<p class="left bot" style="left: -3px;top: 50%;margin-top: -4px;cursor: w-resize"></p><p class="right bot" style="right: -3px;top: 50%;margin-top: -4px;cursor: e-resize"></p><p class="bottom bot" style="bottom: -3px;left: 50%;margin-left: -4px;cursor: s-resize"></p><p class="top bot" style="top: -3px;left: 50%;margin-left: -4px;cursor: n-resize"></p>' + '<p class="left-top bot" style="left: -3px;top: -3px;cursor: nw-resize"></p><p class="left-bottom bot" style="left: -3px;bottom: -3px;cursor: sw-resize"></p><p class="right-top bot" style="right: -3px;top: -3px;cursor: ne-resize"></p><p class="right-bottom bot"style="right: -3px;bottom: -3px;cursor: se-resize"></p></p></p>'); var screen=$('<p id="photoCropBox-bg" style="background: rgba(0,0,0,.5);position: absolute;left: 0;top: 0;width: 100%;height: 100%;z-index: 4;cursor: crosshair;display: none"></p>') _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<opt.fixedScale*_h){ _h=_w/opt.fixedScale } } resizeCropBox(_w,_h,_y,_x); } var c=document.getElementById("cropCanvas"); var ctx=c.getContext("2d"); var img=$('#dataImg'); function getImage() { var scale=$('#photoCropBox-img').width()/$('#dataImg').width(); var sx=parseInt(_corp.css('left'))/scale; var sy=parseInt(_corp.css('top'))/scale; var swidth=parseInt(_corp.css('width'))/scale; var sheight=parseInt(_corp.css('height'))/scale; var c_img = new Image; c_img.onload = function () { ctx.drawImage(c_img,sx,sy,swidth,sheight,0,0,swidth,sheight); var url=c.toDataURL("image/jpeg"); opt.callBack(url); }; c_img.crossOrigin = 'anonymous'; //可选值:anonymous,* c_img.src = imgSrc c.width = swidth; c.height = sheight; } //宽,高,top,left,m-是否是拖拽 function resizeCropBox(w,h,t,l,m) { _corp.css(prefix()+'transition','all 0s'); if(!m){ _corp.css({ width:w, height:h, top:t+'px', left:l+'px' }) }else { _corp.css({ top:t+'px', left:l+'px' }) } cropBoxLine.find('img').css({ top:-t+'px', left:-l+'px' }) } function closeBox() { $('#photoCropBox').remove(); } function prefix() { var prefixes=['','-ms-','-moz-','-webkit-','-o-'],i=0; while (i < prefixes.length){ if($('body').css(prefixes[i]+'transition')){ return prefixes[i]; } i++; } } } })(jQuery)
3.綁定程式碼
$(function () { $('.source').on('click',function () { $(this).photoCrop({ fixedScale:false, isHead:false, callBack:function(url){ $('.target').attr('src',url) }, }); }) })
2.寬高比例固定效果
程式碼:
$(function () { $('.source').on('click',function () { $(this).photoCrop({ fixedScale:5/6, isHead:false, callBack:function(url){ $('.target').attr('src',url) }, }); }) })
3.頭像裁切效果
程式碼:
$(function () { $('.source').on('click',function () { $(this).photoCrop({ fixedScale:1, isHead:true, callBack:function(url){ $('.target').attr('src',url) }, }); }) })
大家學會了嗎?趕快動手嘗試。
相關推薦:
jQuery外掛ImgAreaSelect實作頭像上傳預覽與裁切功能
#以上是jquery實作自訂圖片裁切功能代碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

有些用戶發現圖片上的某些東西是歪的,沒辦法直接框選裁剪,想知道有沒有什麼方法可以把圖片上的東西擺正呢?其實這個操作對於ps高手來說非常簡單,這裡小編給ps小白用戶講講photoshop把歪的圖片裁剪成正的,這個方法很容易操作,希望能幫到大家。 ps裁切傾斜照片教學 1、打開Photoshop,滑鼠移至左側的裁切工具,然後點選滑鼠右鍵,選擇「透視剪切工具」。 2、選擇需要擺正的圖片,確定好四個點。 3、然後按下回車鍵,即可成功地擺正。 4、這樣一來照片上的東西就變正了,而

1.下圖是edius預設的螢幕佈局,預設的EDIUS視窗佈局是橫向版式,因此在單一顯示器環境中,許多視窗是重疊在一起的,且預覽視窗為單一視窗模式。 2、您可以透過【檢視】選單列啟用【雙視窗模式】,使預覽視窗同時顯示播放視窗和錄製視窗。 3.您可以透過【檢視功能表列>視窗佈局>常規】來恢復預設螢幕佈局。另外您也可以自訂適合您的佈局方式,並儲存為常用螢幕佈局:將視窗拖曳成適合自己的佈局,然後點擊【檢視>視窗佈局>儲存目前佈局>新建】,在彈出的【儲存目前佈局】小視窗中輸入佈局名稱,按確定

jQuery中如何使用PUT請求方式?在jQuery中,發送PUT請求的方法與發送其他類型的請求類似,但需要注意一些細節和參數設定。 PUT請求通常用於更新資源,例如更新資料庫中的資料或更新伺服器上的檔案。以下是在jQuery中使用PUT請求方式的具體程式碼範例。首先,確保引入了jQuery庫文件,然後可以透過以下方式發送PUT請求:$.ajax({u

在excel表格中,有時候可能需要插入座標軸,能夠更直觀地看到資料的變化趨勢。還有些小夥伴不清楚怎麼在表中插入座標軸,接下來小編就跟大家分享一下excel自訂座標軸刻度的方法吧。 座標軸插入方法: 1、在excel介面中,選取資料。 2、在插入介面中,點選插入長條圖或長條圖。 3、在展開的介面中,選擇形圖類型。 4、在表格右鍵介面中,點選選擇資料。 5、在展開的介面中,進行自訂即可。

jQuery如何移除元素的height屬性?在前端開發中,經常會遇到需要操作元素的高度屬性的需求。有時候,我們可能需要動態改變元素的高度,而有時候又需要移除元素的高度屬性。本文將介紹如何使用jQuery來移除元素的高度屬性,並提供具體的程式碼範例。在使用jQuery操作高度屬性之前,我們首先需要了解CSS中的height屬性。 height屬性用於設定元素的高度

標題:jQuery小技巧:快速修改頁面所有a標籤的文字在網頁開發中,我們經常需要對頁面中的元素進行修改和操作。使用jQuery時,有時候需要一次修改頁面中所有a標籤的文字內容,這樣可以節省時間和精力。以下將介紹如何使用jQuery快速修改頁面所有a標籤的文本,同時給出具體的程式碼範例。首先,我們需要引入jQuery庫文件,確保在頁面中引入了以下程式碼:<

標題:使用jQuery修改所有a標籤的文字內容jQuery是一款受歡迎的JavaScript庫,被廣泛用於處理DOM操作。在網頁開發中,經常會遇到需要修改頁面上連結標籤(a標籤)的文字內容的需求。本文將介紹如何使用jQuery來實現這個目標,並提供具體的程式碼範例。首先,我們需要在頁面中引入jQuery庫。在HTML檔案中加入以下程式碼:

如何判斷jQuery元素是否具有特定屬性?在使用jQuery操作DOM元素時,常會遇到需要判斷元素是否具有某個特定屬性的情況。在這種情況下,我們可以藉助jQuery提供的方法來輕鬆實現這項功能。以下將介紹兩種常用的方法來判斷一個jQuery元素是否具有特定屬性,並附上具體的程式碼範例。方法一:使用attr()方法和typeof運算子//判斷元素是否具有特定屬
