您好,欢迎访问一九零五行业门户网

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(?)    //拼装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代码
复制代码 代码如下:
这里面的html的头式不能丢的,如果丢了,会在ie7下有问题,不影响使用,但是影响美观度
插件打包下载地址 http://www.jb51.net/jiaoben/31986.html
其它类似信息

推荐信息