DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jquery imgareaselect 使用利用js與程序結合實現圖片剪切
jquery imgareaselect 使用利用js與程序結合實現圖片剪切
編輯:JQuery特效代碼     

文件打包下載

 

 

代碼如下:
/*
缺陷,當前在ff3下,用jquery的 width()與height()函數,在不設置圖片的寬度與高度的時候,不能取到
需要在圖片load函數裡面初始化才可以
*/
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;
}
//建立預覽圖片
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;
}
//初始化函數
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());
//設置預覽加載層樣式
jQuery("#"+this.view_div_id).css({'width':this.view_width,'height':this.view_height,'overflow':'hidden'});
//構造選擇區域完成的函數
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 函數
var ias = this.pic_obj.imgAreaSelect({
//設置選擇框的比列
//aspectRatio:"1:1",
//設置框的添加效果
fadeSpeed:200,
//選擇框選擇完畢是否自己取消
autoHide:false,
//是否顯示圖片遮罩層
show:true,
//是否采用api
instance: true,
//設置初始函數 畫出選擇框
onInit:function(img, selection){ias.setSelection(100, 50, 250, 150, true);ias.update();},
//設置選擇完畢的函數,采用了動態執行
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);
});
//判斷,是否有參數
post_page += post_page.lastIndexOf("?")<0 ? "?" : "&";
//拼裝get方式的url
post_url = post_page+post_arr.join("&");
alert(post_url);
}

這個是封裝後js代碼
下面看看這段代碼如何使用
代碼如下:
$(document).ready(function () {

//聲明函數
var sanshi_img = new sanshi_imgareaselect("mypic","preview");
//確保圖片加載完成執行初始化函數,這樣避免上面的提到的bug,否則不能保證兼容性
$("#mypic").load(function(){sanshi_img.init();});
//監聽保存事件
$("#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的頭式不能丟的,如果丟了,會在ie7下有問題,不影響使用,但是影響美觀度
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved