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

 更新时间:2009年07月30日 18:21:33   作者:  
当前在ff3下,用jquery的 width()与height()函数,在不设置图片的宽度与高度的时候,不能取到 需要在图片load函数里面初始化才可以
复制代码 代码如下:

/*
缺陷,当前在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://www.jb51.net/jiaoben/31986.html

相关文章

  • 有关jQuery中parent()和siblings()的小问题

    有关jQuery中parent()和siblings()的小问题

    本文通过一个实例给大家介绍有关parent()和siblings()问题原因及解决方案,非常不错具有参考借鉴价值,感兴趣的朋友一起看看吧
    2016-06-06
  • jquery编写Tab选项卡滚动导航切换特效

    jquery编写Tab选项卡滚动导航切换特效

    这篇文章主要为大家详细介绍了jquery编写Tab选项卡滚动导航切换特效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • 使用jquery给input和textarea设定ie中的focus

    使用jquery给input和textarea设定ie中的focus

    在浏览网站的过程中,我们经常可以看见当鼠标点击后样式改变的文本框和文本域,那么这种效果是如何实现的呢?
    2008-05-05
  • jQuery实现别踩白块儿网页版小游戏

    jQuery实现别踩白块儿网页版小游戏

    本文主要介绍了jQuery实现别踩白块儿网页版小游戏的思路分析与代码。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • imgAreaSelect 中文文档帮助说明

    imgAreaSelect 中文文档帮助说明

    ImgAreaSelect是一jQuery插件,它支持用户通过鼠标拖曳选择图片的一部分,非常的fashion。另外,可以在这个选择图像区域的基础上应用网站的其他一些技术,比如图片拖曳、图片编辑等。
    2011-10-10
  • 简单实现jQuery上传图片显示预览功能

    简单实现jQuery上传图片显示预览功能

    这篇文章主要教大家如何简单实现jQuery上传图片显示预览功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • jquery 页面滚动到指定DIV实现代码

    jquery 页面滚动到指定DIV实现代码

    页面滚动到指定DIV的方法有很多,在本文将为大家介绍下jquery是如何实现的
    2013-09-09
  • jquery实现拖拽小方块效果

    jquery实现拖拽小方块效果

    这篇文章主要为大家详细介绍了jquery实现拖拽小方块效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • slideToggle+slideup实现手机端折叠菜单效果

    slideToggle+slideup实现手机端折叠菜单效果

    这篇文章主要为大家详细介绍了slideToggle+slideup实现手机端折叠菜单效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Jquery插件编写简明教程

    Jquery插件编写简明教程

    这篇文章主要介绍了Jquery插件编写简明教程,jquery插件开发实例,需要的朋友可以参考下
    2014-03-03

最新评论