js+jquery实现图片裁剪功能

2020-05-22 16:21:09易采站长站整理

     <div action=”left” class=”rLeft”></div>
     <div action=”up” class=”rUp”></div>
     <div action=”down” class=”rDown” ></div>
       </div>’)
   .bind(“mousedown.r”,function(e){self.start(e)})
   .appendTo(this.container[0]);
   this.tips = $(‘<span class=”‘+this.tipsClass+'” />’).appendTo(this.container[0]);
   this.setImg();
  },
  setImg : function(){
   var block = this.block;
   var left  = block.css(“left”),
    top   = block.css(“top”),
    height = block.height(),
    width  = block.width();
   this.imgC.css({
    height: height,
    width : width,
    “background-position” : “-“+left+” -“+top
   });
   this.tips.html(“left:<span>”+parseInt(left) + “</span>top:<span>” + +parseInt(top) + “</span>width:<span>”+width+ “</span>height:<span>”+height+”</span>”);
  },
  start : function(e){      
   var $elem     = $(e.target),
    block     = this.block,
    self      = this,
    move      = false,
    container = this.container,
    action    = $elem.attr(“action”);
   //这个 每次都要计算 基本dom结构的改变 浏览器的缩放 都会让里面的值发生改变 
   this.offset = $.extend({height:container.height(),width:container.width()},container.offset());
   this.blockOriginal = {height:block.height(),width:block.width(),left:parseInt(block.css(“left”)),top:parseInt(block.css(“top”))};
   if(action){
    this.fun = this[action];    
   }else{
    this.x = e.clientX – this.offset.left – this.blockOriginal.left ;
    this.y = e.clientY – this.offset.top – this.blockOriginal.top;
    move = true;
   }   
   ie
    &&this.block[0].setCapture();