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 中 data 方法的误解分析
Jun 18 Javascript
jQuery ajax时间差导致的变量赋值问题分析
Jan 22 Javascript
javascript表单处理具体实现代码(表单、链接、按钮)
May 07 Javascript
全面了解构造函数继承关键apply call
Jul 26 Javascript
轻松掌握JavaScript装饰者模式
Aug 27 Javascript
浅谈jQuery hover(over, out)事件函数
Dec 03 Javascript
jQuery EasyUI ProgressBar进度条组件
Feb 28 Javascript
layui前段框架日期控件使用方法详解
May 19 Javascript
JavaScript实现多文件下载方法解析
Aug 07 Javascript
vue内置组件keep-alive事件动态缓存实例
Oct 30 Javascript
vue中data改变后让视图同步更新的方法
Mar 29 Vue.js
javascript遍历对象的五种方式实例代码
Oct 24 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
php 什么是PEAR?
2009/03/19 PHP
asp(javascript)全角半角转换代码 dbc2sbc
2009/08/06 Javascript
jQuery EasyUI API 中文文档 搜索框
2011/09/29 Javascript
javascript向flash swf文件传递参数值注意细节
2012/12/11 Javascript
基于jquery实现点击左右按钮图片横向滚动
2013/04/11 Javascript
一些老手都不一定知道的JavaScript技巧
2014/05/06 Javascript
鼠标悬浮停留三秒后自动显示大图js代码
2014/09/09 Javascript
一个字符串反转函数可实现字符串倒序
2014/09/15 Javascript
JavaScript随机生成信用卡卡号的方法
2015/04/07 Javascript
在ASP.NET MVC项目中使用RequireJS库的用法示例
2016/02/15 Javascript
AngularJS实用开发技巧(推荐)
2016/07/13 Javascript
jQuery Ajax全解析
2017/02/13 Javascript
微信小程序实现通过js操作wxml的wxss属性示例
2018/12/06 Javascript
详解vue beforeRouteEnter 异步获取数据给实例问题
2019/08/09 Javascript
webpack+vue.js构建前端工程化的详细教程
2020/05/10 Javascript
原生JavaScript实现随机点名表
2021/01/14 Javascript
vue+flask实现视频合成功能(拖拽上传)
2021/03/04 Vue.js
[58:18]2018DOTA2亚洲邀请赛3月29日 小组赛B组 iG VS Mineski
2018/03/30 DOTA
python实现排序算法
2014/02/14 Python
numpy 进行数组拼接,分别在行和列上合并的实例
2018/05/08 Python
python中的json总结
2018/10/11 Python
Python中利用aiohttp制作异步爬虫及简单应用
2018/11/29 Python
Python的UTC时间转换讲解
2019/02/26 Python
python被修饰的函数消失问题解决(基于wraps函数)
2019/11/04 Python
python保存log日志,实现用log日志画图
2019/12/24 Python
世界上最受欢迎的花店:1-800-Flowers.com
2020/06/01 全球购物
JENNIFER BEHR官网:各种耳环和发饰
2020/06/07 全球购物
应用服务器有那些
2012/01/19 面试题
中专毕业生自我鉴定
2013/11/21 职场文书
统计系教授推荐信
2014/02/28 职场文书
气象学专业个人求职信
2014/04/22 职场文书
小学生安全演讲稿
2014/04/25 职场文书
勇敢的心观后感
2015/06/09 职场文书
Python竟然能剪辑视频
2021/05/25 Python
JS实现数组去重的11种方法总结
2022/04/04 Javascript
html解决浏览器记住密码输入框的问题
2023/05/07 HTML / CSS