【发布时间】:2015-08-07 12:32:39
【问题描述】:
我有一个有序数组:
btnDrag.pos = [0, 65, 131, 196, 259, 323, 388, 453, 517];
还有一个在拖动停止时触发的函数:
btnDrag.draggable({
axis: 'x',
containment: 'parent',
stop: function() {
var index = (function(){
var new_x = btnDrag.position().left;
// now, how to find the closest index in btnDrag.pos relative to new_x ?
// return index;
})();
btnDrag.animate({
'left': (btnDrag.pos[index] + 'px')
});
}
});
数组值是允许 btnDrag 停留的点(在轴“x”中)。
因此,该函数必须返回与 btnDrag go 值最近的索引。
提前致谢。
【问题讨论】:
-
请包含一个小提琴的视觉效果。
-
检查这个小提琴-http://jsfiddle.net/dsov5am8/。我的回答与第二个类似。所以发表评论。
标签: javascript algorithm find