代码之家  ›  专栏  ›  技术社区  ›  Mircea

jQuery在拖动时获取上一个和下一个元素

  •  3
  • Mircea  · 技术社区  · 16 年前

    我想做一个时间表。时间轴几乎没有将绝对位置放置在随机位置上的动态关键帧元素。

    我有个演示 http://jsfiddle.net/3pXC9/1/ 只要把红条拖来拖去,我就可以根据位置得到它的“兄弟姐妹”。

    谢谢你

    1 回复  |  直到 16 年前
        1
  •  2
  •   Simen Echholt    16 年前

    像这样的?

    演示: 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
            */
    }
    
    推荐文章