1

I have a draggable container that holds a varying amount of children displayed side by side with display: inline-block. I want the containment to be based on the offset of either the first or last child, so the user is able to drag all but one (either first or last) offscreen in either direction.

drag: function(){//prevent dragging once last element offset is within 10% of window width
  if ($('.element:last').offset().left < $(window).width() * .10)){
    return false;
  }
}

The problem is that once I return false, I can no longer drag. Some variation of containment seems like the only option here, but I'm not quite sure how to feed it such a complicated condition.

4

1 回答 1

0

我最终做的是把所有东西都放在一个传递给draggable方法的对象中。一旦我有了对象,只要孩子的位置发生变化,我就可以更新包含属性。设置好后,我可以重新初始化draggable.

var borg = {
  axis: 'x',
  drag:function(){}
}
$('#draggable').draggable(borg);
//set property after each time the child offset changes
borg.containment = [
  -1 * ($('.child:first').offset().left + $(window).width() * .18), //x1
  0,          
  $(window).width()/2, //x2
  0
];
//reinitialize
$('#draggable').draggable(borg);
//would $('#draggable').draggable('destroy').draggable(borg) be better or unnecessary?
于 2013-09-11T03:05:22.277 回答