jquery imgareaselect 使用利用js与程序结合实现图片剪切


Posted in Javascript onJuly 30, 2009
/* 
缺陷,当前在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下有问题,不影响使用,但是影响美观度
插件打包下载地址 https://3water.com/jiaoben/31986.html
Javascript 相关文章推荐
jQuery学习基础知识小结
Nov 25 Javascript
jquery Tab效果和动态加载的简单实例
Dec 11 Javascript
javaScript 页面自动加载事件详解
Feb 10 Javascript
jQuery实现信息提示框(带有圆角框与动画)效果
Aug 07 Javascript
用jQuery旋转插件jqueryrotate制作转盘抽奖
Feb 10 Javascript
jQuery开源组件BootstrapValidator使用详解
Jun 29 jQuery
微信小程序 websocket 实现SpringMVC+Spring+Mybatis
Aug 04 Javascript
基于react后端渲染模板引擎noox发布使用
Jan 11 Javascript
linux 后台运行node服务指令方法
May 23 Javascript
Angularjs之如何在跨域请求中传输Cookie的方法
Jun 01 Javascript
JavaScript中工厂函数与构造函数示例详解
May 06 Javascript
Vue中通过Vue.extend动态创建实例的方法
Aug 13 Javascript
Javascript this指针
Jul 30 #Javascript
javascript 进度条 实现代码
Jul 30 #Javascript
JS input 数字验证代码
Jul 30 #Javascript
关于取不到由location.href提交而来的上级页面地址的解决办法
Jul 30 #Javascript
window.parent调用父框架时 ie跟火狐不兼容问题
Jul 30 #Javascript
javascript EXCEL 操作类代码
Jul 30 #Javascript
JavaScript this 深入理解
Jul 30 #Javascript
You might like
MySQL中create table语句的基本语法是
2007/01/15 PHP
PHP脚本中include文件出错解决方法
2008/11/20 PHP
php数据库配置文件一般做法分享
2012/07/07 PHP
PHP处理SQL脚本文件导入到MySQL的代码实例
2014/03/17 PHP
php生成txt文件实例代码介绍
2016/04/28 PHP
extjs 的权限问题 要求控制的对象是 菜单,按钮,URL
2010/03/09 Javascript
javascript中使用css需要注意的地方小结
2010/09/01 Javascript
DD_belatedPNG,IE6下PNG透明解决方案(国外)
2010/12/06 Javascript
Js数组的操作push,pop,shift,unshift等方法详细介绍
2012/12/28 Javascript
Jquery Ajax xmlhttp请求成功问题
2015/02/04 Javascript
原生js和jquery实现图片轮播淡入淡出效果
2015/04/23 Javascript
javascript限制文本框输入值类型的方法
2015/05/07 Javascript
jQuery常用且重要方法汇总
2015/07/13 Javascript
NodeJs读取JSON文件格式化时的注意事项
2016/09/25 NodeJs
canvas绘制的直线动画
2017/01/23 Javascript
如何在微信小程序里面退出小程序的方法
2019/04/28 Javascript
Vue 列表上下过渡效果的实例代码
2019/06/25 Javascript
ionic2.0双击返回键退出应用
2019/09/17 Javascript
如何构建一个Vue插件并生成npm包
2020/10/26 Javascript
[01:00:53]2018DOTA2亚洲邀请赛3月29日 小组赛B组 iG VS Secret
2018/03/30 DOTA
python通过exifread模块获得图片exif信息的方法
2015/03/16 Python
python轻松实现代码编码格式转换
2015/03/26 Python
python版简单工厂模式
2017/10/16 Python
python创建文件时去掉非法字符的方法
2018/10/31 Python
Python lxml解析HTML并用xpath获取元素的方法
2019/01/02 Python
python语言元素知识点详解
2019/05/15 Python
Python Django Vue 项目创建过程详解
2019/07/29 Python
Python+pyftpdlib实现局域网文件互传
2020/08/24 Python
python GUI计算器的实现
2020/10/09 Python
CSS3 渐变(Gradients)之CSS3 线性渐变
2016/07/08 HTML / CSS
彪马香港官方网上商店:PUMA香港
2020/12/06 全球购物
如何提高MySql的安全性
2014/06/19 面试题
求职信标题怎么写
2014/05/26 职场文书
办公室管理规章制度
2015/08/04 职场文书
MySQL 如何限制一张表的记录数
2021/09/14 MySQL