jquery imgareaselect 使用利用js與程序結(jié)合實(shí)現(xiàn)圖片剪切
/*
缺陷,當(dāng)前在ff3下,用jquery的 width()與height()函數(shù),在不設(shè)置圖片的寬度與高度的時(shí)候,不能取到
需要在圖片load函數(shù)里面初始化才可以
*/
sanshi_imgareaselect = function(pic_id,view_div_id){
this.pic_obj = jQuery("#"+pic_id);
this.pic_width;
this.pic_height;
this.view_div_id = view_div_id;
this.view_width = 100;
this.view_height = 100;
this.view_img_id = view_div_id+"_sanshi_img";
this.ias;
}
//建立預(yù)覽圖片
sanshi_imgareaselect.prototype.make_view_pic =function(){
var img_obj = jQuery(document.createElement("IMG"));
img_obj.attr("src",this.pic_obj.attr("src"));
img_obj.attr("id",this.view_img_id);
img_obj.attr("width",this.view_width);
img_obj.attr("height",this.view_height);
return img_obj;
}
//初始化函數(shù)
sanshi_imgareaselect.prototype.init=function(){
this.pic_width = this.pic_obj.attr("width");
this.pic_height = this.pic_obj.attr("height");
//alert(this.pic_width+":"+this.pic_height);
//添加圖片
jQuery("#"+this.view_div_id).append(this.make_view_pic());
//設(shè)置預(yù)覽加載層樣式
jQuery("#"+this.view_div_id).css({'width':this.view_width,'height':this.view_height,'overflow':'hidden'});
//構(gòu)造選擇區(qū)域完成的函數(shù)
var fun_str="if ( selection.width && selection.height){ var scaleX = "+this.view_width+" / selection.width;var scaleY = "+this.view_height+" / selection.height;jQuery('#"+this.view_img_id+"').css({width: Math.round(scaleX * "+this.pic_width+"),height: Math.round(scaleY * "+this.pic_height+"),marginLeft: -Math.round(scaleX * selection.x1),marginTop: -Math.round(scaleY * selection.y1)});}";
//alert(fun_str);
//初始化imgAreaSelect 函數(shù)
var ias = this.pic_obj.imgAreaSelect({
//設(shè)置選擇框的比列
//aspectRatio:"1:1",
//設(shè)置框的添加效果
fadeSpeed:200,
//選擇框選擇完畢是否自己取消
autoHide:false,
//是否顯示圖片遮罩層
show:true,
//是否采用api
instance: true,
//設(shè)置初始函數(shù) 畫(huà)出選擇框
onInit:function(img, selection){ias.setSelection(100, 50, 250, 150, true);ias.update();},
//設(shè)置選擇完畢的函數(shù),采用了動(dòng)態(tài)執(zhí)行
onSelectEnd:function(img, selection){eval(fun_str);}
});
//賦值給全局
this.ias = ias;
}
//保存事件 采用的是get方式提交
sanshi_imgareaselect.prototype.save_pic=function(post_page,post_param){
var opt = this.ias.getSelection(true);
var post_arr = new Array();
jQuery.each(post_param,function(i,n){
var temp_str =i+"=";
temp_str += opt[n] ? opt[n] : n;
post_arr.push(temp_str);
});
//判斷,是否有參數(shù)
post_page += post_page.lastIndexOf("?")<0 ? "?" : "&";
//拼裝get方式的url
post_url = post_page+post_arr.join("&");
alert(post_url);
}
這個(gè)是封裝后js代碼
下面看看這段代碼如何使用
$(document).ready(function () {
//聲明函數(shù)
var sanshi_img = new sanshi_imgareaselect("mypic","preview");
//確保圖片加載完成執(zhí)行初始化函數(shù),這樣避免上面的提到的bug,否則不能保證兼容性
$("#mypic").load(function(){sanshi_img.init();});
//監(jiān)聽(tīng)保存事件
$("#save_pic").click(function(){
sanshi_img.save_pic('1.php?n=6',{"id":5,"px1":"x1","py1":"y1",'px2':"x2","py2":"y2",'pwidth':"width",'pheight':"height"});
});
})
下面看下html代碼
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="css/imgareaselect-animated.css" href="css/imgareaselect-animated.css" />
<script type="text/javascript" src="jquery-1.3.2.min.js" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery.imgareaselect.pack.js" src="scripts/jquery.imgareaselect.pack.js"></script>
</head>
<body>
<div id="s">
<img id="mypic" name="mypic" dt="sanshi" src="scott-h-biram.jpg" src="scott-h-biram.jpg" title="mypic"/>
</div>
<div id="preview"></div>
<div><input type="button" id="save_pic" value="保存"></div>
</body>
</html>
這里面的html的頭式不能丟的,如果丟了,會(huì)在ie7下有問(wèn)題,不影響使用,但是影響美觀度
插件打包下載地址 http://www.dbjr.com.cn/jiaoben/31986.html
相關(guān)文章
jQuery實(shí)現(xiàn)的點(diǎn)擊按鈕改變樣式功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的點(diǎn)擊按鈕改變樣式功能,涉及jQuery基于事件響應(yīng)的頁(yè)面元素樣式動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-07-07jQuery文本框得到與失去焦點(diǎn)動(dòng)態(tài)改變樣式效果
這篇文章主要介紹了jQuery文本框得到與失去焦點(diǎn)動(dòng)態(tài)改變樣式效果,涉及jQuery針對(duì)頁(yè)面表單元素樣式的動(dòng)態(tài)操作技巧,需要的朋友可以參考下2016-09-09jquery中dom操作和事件的實(shí)例學(xué)習(xí) 仿yahoo郵箱登錄框的提示效果
最近把jquery中的dom操作和jquery中的事件和動(dòng)畫(huà)的方法都大體測(cè)了一下。本來(lái)想細(xì)細(xì)的把每個(gè)方法都寫(xiě)出來(lái)介紹下2011-11-11jQuery中event.target和this的區(qū)別詳解
這篇文章主要介紹了jQuery中event.target和this的區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08jquery+css3實(shí)現(xiàn)的經(jīng)典彈出層效果示例
這篇文章主要介紹了jquery+css3實(shí)現(xiàn)的經(jīng)典彈出層效果,結(jié)合實(shí)例形式分析了jquery+css3實(shí)現(xiàn)彈出層具體原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下2020-05-05jquery實(shí)現(xiàn)拖拽table表頭改變列寬
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)拖拽table表頭改變列寬,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02通過(guò)jquery實(shí)現(xiàn)tab標(biāo)簽瀏覽效果
這篇文章主要介紹了通過(guò)jquery實(shí)現(xiàn)tab標(biāo)簽瀏覽效果2007-02-02JQuery入門(mén)—JQuery程序的代碼風(fēng)格詳細(xì)介紹
良好的代碼風(fēng)格可以有效的增加可讀性,本節(jié)為大家介紹jQuery程序的代碼風(fēng)格,感興趣的朋友可以參考下2013-01-01jQuery+canvas實(shí)現(xiàn)的球體平拋及顏色動(dòng)態(tài)變換效果
這篇文章主要介紹了jQuery+canvas實(shí)現(xiàn)的球體平拋及顏色動(dòng)態(tài)變換效果,通過(guò)jQuery結(jié)合html5的canvas技術(shù)調(diào)用時(shí)間函數(shù)實(shí)時(shí)進(jìn)行數(shù)學(xué)運(yùn)算,最終實(shí)現(xiàn)球體平拋及顏色動(dòng)態(tài)變換的效果,需要的朋友可以參考下2016-01-01