我只是在为另一个问题玩类似的东西,我认为同样的方法可能对你有用。
基本上我想出了一种将新的snapModes添加到 jQuery-UI 的方法。
您将需要编辑 jQuery-ui 文件,但我认为这可能是值得的。
除了标准的内部,外部和两者。我添加了以下选项:
工作示例
就是这样:
搜索:
if(o.snapMode != 'inner') {
var ts = Math.abs(t - y2) <= d;
var bs = Math.abs(b - y1) <= d;
var ls = Math.abs(l - x2) <= d;
var rs = Math.abs(r - x1) <= d;
if(ts) ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
if(bs) ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top - inst.margins.top;
if(ls) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left - inst.margins.left;
if(rs) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left - inst.margins.left;
}
var first = (ts || bs || ls || rs);
if(o.snapMode != 'outer') {
var ts = Math.abs(t - y1) <= d;
var bs = Math.abs(b - y2) <= d;
var ls = Math.abs(l - x1) <= d;
var rs = Math.abs(r - x2) <= d;
if(ts) ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top - inst.margins.top;
if(bs) ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
if(ls) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left - inst.margins.left;
if(rs) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left - inst.margins.left;
}
并将其替换为:
if (o.snapMode != 'inner' && o.snapMode != 'innerTop' && o.snapMode != 'innerBottom' && o.snapMode != 'innerLeft' && o.snapMode != 'innerRight' && o.snapMode != 'outerTop' && o.snapMode != 'outerBottom' && o.snapMode != 'outerLeft' && o.snapMode != 'outerRight') {
var ts = Math.abs(t - y2) <= d;
var bs = Math.abs(b - y1) <= d;
var ls = Math.abs(l - x2) <= d;
var rs = Math.abs(r - x1) <= d;
if (ts) ui.position.top = inst._convertPositionTo("relative", {
top: t - inst.helperProportions.height,
left: 0
}).top - inst.margins.top;
if (bs) ui.position.top = inst._convertPositionTo("relative", {
top: b,
left: 0
}).top - inst.margins.top;
if (ls) ui.position.left = inst._convertPositionTo("relative", {
top: 0,
left: l - inst.helperProportions.width
}).left - inst.margins.left;
if (rs) ui.position.left = inst._convertPositionTo("relative", {
top: 0,
left: r
}).left - inst.margins.left;
}
var first = (ts || bs || ls || rs);
if (o.snapMode != 'outer' && o.snapMode != 'innerTop' && o.snapMode != 'innerBottom' && o.snapMode != 'innerLeft' && o.snapMode != 'innerRight' && o.snapMode != 'outerTop' && o.snapMode != 'outerBottom' && o.snapMode != 'outerLeft' && o.snapMode != 'outerRight') {
var ts = Math.abs(t - y1) <= d;
var bs = Math.abs(b - y2) <= d;
var ls = Math.abs(l - x1) <= d;
var rs = Math.abs(r - x2) <= d;
if (ts) ui.position.top = inst._convertPositionTo("relative", {
top: t,
left: 0
}).top - inst.margins.top;
if (bs) ui.position.top = inst._convertPositionTo("relative", {
top: b - inst.helperProportions.height,
left: 0
}).top - inst.margins.top;
if (ls) ui.position.left = inst._convertPositionTo("relative", {
top: 0,
left: l
}).left - inst.margins.left;
if (rs) ui.position.left = inst._convertPositionTo("relative", {
top: 0,
left: r - inst.helperProportions.width
}).left - inst.margins.left;
}
if (o.snapMode == 'innerTop') {
var ts = Math.abs(t - y1) <= d;
if (ts) ui.position.top = inst._convertPositionTo("relative", {
top: t,
left: 0
}).top - inst.margins.top;
}
if (o.snapMode == 'innerBottom') {
var bs = Math.abs(b - y2) <= d;
if (bs) ui.position.top = inst._convertPositionTo("relative", {
top: b - inst.helperProportions.height,
left: 0
}).top - inst.margins.top;
}
if (o.snapMode == 'innerLeft') {
var ls = Math.abs(l - x1) <= d;
if (ls) ui.position.left = inst._convertPositionTo("relative", {
top: 0,
left: l
}).left - inst.margins.left;
}
if (o.snapMode == 'innerRight') {
var rs = Math.abs(r - x2) <= d;
if (rs) ui.position.left = inst._convertPositionTo("relative", {
top: 0,
left: r - inst.helperProportions.width
}).left - inst.margins.left;
}
if (o.snapMode == 'outerTop') {
var ts = Math.abs(t - y2) <= d;
if (ts) ui.position.top = inst._convertPositionTo("relative", {
top: t - inst.helperProportions.height,
left: 0
}).top - inst.margins.top;
}
if (o.snapMode == 'outerBottom') {
var bs = Math.abs(b - y1) <= d;
if (bs) ui.position.top = inst._convertPositionTo("relative", {
top: b,
left: 0
}).top - inst.margins.top;
}
if (o.snapMode == 'outerLeft') {
var ls = Math.abs(l - x2) <= d;
if (ls) ui.position.left = inst._convertPositionTo("relative", {
top: 0,
left: l - inst.helperProportions.width
}).left - inst.margins.left;
}
if (o.snapMode == 'outerRight') {
var rs = Math.abs(r - x1) <= d;
if (rs) ui.position.left = inst._convertPositionTo("relative", {
top: 0,
left: r
}).left - inst.margins.left;
}