drag: function (event, ui) {
//迭代所有的guids,记住最近的h和v guids
var guideV, guideH, distV = MIN_DISTANCE + 1,
distH = MIN_DISTANCE + 1,
offsetV, offsetH;
var chosenGuides = {
top: {
dist: MIN_DISTANCE + 1
},
left: {
dist: MIN_DISTANCE + 1
}
};
var $t = $(this);
//pageX、pageY:文档坐标x、y ;
var pos = {
top: (event.pageY - $(‘.draggable_list‘).offset().top) - innerOffsetY ,
left: (event.pageX - $(‘.draggable_list‘).offset().left) - innerOffsetX
};
//outerHeight、outerWidth:整个浏览器的高度、宽度
var w = $t.outerWidth() - 1;
var h = $t.outerHeight() - 1;
var elemGuides = computeGuidesForElement(null, pos, w, h);
$.each(guides, function (i, guide) {
$.each(elemGuides, function (i, elemGuide) {
if (guide.type == elemGuide.type) {
var prop = guide.type == "h" ? "top" : "left";
var d = Math.abs(elemGuide[prop] - guide[prop]);
if (d < chosenGuides[prop].dist) {
chosenGuides[prop].dist = d;
chosenGuides[prop].offset = elemGuide[prop] - pos[prop];
chosenGuides[prop].guide = guide;
}
}
});
});
if (chosenGuides.top.dist <= MIN_DISTANCE) {
$("#guide-h").css("top", chosenGuides.top.guide.top).show();
ui.position.top = chosenGuides.top.guide.top - chosenGuides.top.offset;
} else {
$("#guide-h").hide();
ui.position.top = pos.top;
}
if (chosenGuides.left.dist <= MIN_DISTANCE) {
$("#guide-v").css("left", chosenGuides.left.guide.left).show();
ui.position.left = chosenGuides.left.guide.left - chosenGuides.left.offset;
} else {
$("#guide-v").hide();
ui.position.left = pos.left;
}
},
stop: function (event, ui) {
$("#guide-v, #guide-h").hide();
}