我先貼上我的做法,是個回調函數 rangeMoveFunc : function(obj,e,x,y){//obj為圖片對象,x為圖片所移動的位置,也就是x = e.pageX//當前移動座標 - (downX//mousedown時的e.pageX; - obj.offset().left) var thisWidth = obj.width(); var thisHeight = obj.height(); var rx,ry;//圖片所能移動的座標,當rx,ry為undefined時, rx = x;ry = y; var left,top,right,bottom;//牆壁的位置 wallObj.each(function(){ var offset = $(this).offset(); switch($(this).attr('pos'))//我定義了牆壁的方向,按牆壁的方向來判斷圖片所移動的位置 { case 'left' : left = offset.left + $(this).width(); top = offset.top - thisHeight; bottom = offset.top + $(this).height(); if(!rx && x < left && y > top && y < bottom){ rx = left; } break; case 'top' : top = offset.top + $(this).height(); left = offset.left - thisWidth; right = offset.left + $(this).width(); if(!ry && y < top && x > left && x < right){ ry = top; } break; case 'right' : right = offset.left - thisWidth; top = offset.top - thisHeight; bottom = offset.top + $(this).height(); if(!rx && x > right && y > top && y < bottom){ rx = right; } break; case 'bottom' : bottom = offset.top - thisHeight; left = offset.left - thisWidth; right = offset.left + $(this).width(); if(!ry && y > bottom && x > left && x < right){ ry = bottom; } break; } if(rx && ry){ return false; } })
如果看不到的人就來這看吧
http://img.my.csdn.net/uploads/201205/31/1338452331_5909.png
rangeMoveFunc : function(obj,e,x,y){//obj為圖片對象,x為圖片所移動的位置,也就是x = e.pageX//當前移動座標 - (downX//mousedown時的e.pageX; - obj.offset().left)
var thisWidth = obj.width();
var thisHeight = obj.height();
var rx,ry;//圖片所能移動的座標,當rx,ry為undefined時, rx = x;ry = y;
var left,top,right,bottom;//牆壁的位置
wallObj.each(function(){
var offset = $(this).offset();
switch($(this).attr('pos'))//我定義了牆壁的方向,按牆壁的方向來判斷圖片所移動的位置
{
case 'left' :
left = offset.left + $(this).width();
top = offset.top - thisHeight;
bottom = offset.top + $(this).height();
if(!rx && x < left && y > top && y < bottom){
rx = left;
}
break;
case 'top' :
top = offset.top + $(this).height();
left = offset.left - thisWidth;
right = offset.left + $(this).width();
if(!ry && y < top && x > left && x < right){
ry = top;
}
break;
case 'right' :
right = offset.left - thisWidth;
top = offset.top - thisHeight;
bottom = offset.top + $(this).height();
if(!rx && x > right && y > top && y < bottom){
rx = right;
}
break;
case 'bottom' :
bottom = offset.top - thisHeight;
left = offset.left - thisWidth;
right = offset.left + $(this).width();
if(!ry && y > bottom && x > left && x < right){
ry = bottom;
}
break;
}
if(rx && ry){
return false;
}
})
return [rx,ry];
}
下載地址:
http://download.csdn.net/download/jxsrsxyx/4343112
大家可以先下載代碼看效果,再更改代碼已致不會移出後在給個鏈接給我或發個Email給我也可以。Thanks