像这样的?
演示:
http://jsfiddle.net/3pXC9/11/
使用的Javascript:
stop: function(event, ui) {
var this_left = $(this).position().left;
var keyframes = $(".keyframe");
var closest_left = [null,0], closest_right = [null,0];
keyframes.each(function(index) {
var t = $(this);
var offset = t.position().left - this_left;
if (offset >= 0) {
if (closest_right[0] == null || offset < closest_right[1]) {
closest_right[0] = t;
closest_right[1] = offset;
}
}
else {
if (closest_left[0] == null || offset > closest_left[1]) {
closest_left[0] = t;
closest_left[1] = offset;
}
}
});
/*
closest_left[0] = closest element to the left (null if none)
closest_left[1] = offset relative to the dragger
the same goes for closest_right
*/
}