简体   繁体   English

继续拖动事件而不将元素拖动到任意阈值以上

[英]Continue drag event without dragging an element further than an arbitrary threshold

I have an element with two markers over it.我有一个上面有两个标记的元素。 The first marker sets minimum value of something, and the second, maximum.第一个标记设置某物的最小值,第二个标记设置最大值。 That said, I want to prevent the "maximum" one be lower than "minimum" one.也就是说,我想防止“最大值”低于“最小值”。 I'm using jQuery UI's draggable as a wrapper around HTML5 drag and drop, and here's my code:我使用 jQuery UI 的可拖动作为 HTML5 拖放的包装器,这是我的代码:

component = {}; // actually, it's a real component but let's pretend it's simply a plain-object.
component.containerWidth = $('.marker').parent().width();

$('.marker').each(() => {
  $(this).draggable({
    containment: 'parent',
    axis: 'y',
    create: (event, ui) => {
      if ($(this).hasClass('min')) {
        component.minMarker = $(this).position().top;
      } else if ($(this).hasClass('max')) {
        component.minMarker = $(this).position().top;
      }
    },
    drag: (event, ui) => {
      const isDraggingMin = $(this).hasClass('min');
      const isDraggingMax = $(this).hasClass('max');
      const isMinLower = ui.position.top + $(this).height() <= component.maxMarker;
      const isMaxHigher = ui.position.top - $(this).height() >= component.minMarker;

      if (isDraggingMin && isMinLower) {
        // set actual markers' positions
        // do something
      } else if (isDraggingMax && isMaxHigher) {
        // set actual markers' positions
        // do something
      } else {
        event.preventDefault();
      }
    }
  });
});

The problem is that, when preventDefault gets invoked, the dragging doesn't work anymore until I stop dragging and start it over.问题是,当preventDefault时,拖动不再起作用,直到我停止拖动并重新开始。 It means, if I drag a "minimum" marker as close to "maximum" marker as event.preventDefault() gets fired, then no matter how and where I drag, the marker stays.这意味着,如果我在event.preventDefault()被触发时将“最小”标记拖动到接近“最大”标记的位置,那么无论我拖动的方式和位置,标记都会保持不变。 If I release it and then start dragging again, it drags until it gets into the same conditions.如果我释放它然后再次开始拖动,它会一直拖动直到它进入相同的条件。

Simply setting css properties via $(...).css({left: ...}) does not work, and perhaps it shouldn't.简单地通过$(...).css({left: ...})设置 css 属性是行不通的,也许不应该。

So how do I solve it then?那么我该如何解决呢? How can I allow dragging without having user to release and grab a marker once in a while when they occasionally approach another marker?当用户偶尔接近另一个标记时,如何允许拖动而无需用户偶尔释放和抓住标记?

The codepen: http://codepen.io/rishatmuhametshin/pen/WrvoKd代码笔: http ://codepen.io/rishatmuhametshin/pen/WrvoKd

I have some other thing in mind for you,, see this code and I hope that can fit in your need,, or at least can help you to see this through another lens (I'm wearing glasses btw :)我有一些其他的想法给你,请看这段代码,我希望它可以满足你的需要,或者至少可以帮助你通过另一个镜头看到这一点(顺便说一句,我戴着眼镜:)

http://codepen.io/mkdizajn/pen/bEdgLQ?editors=001 http://codepen.io/mkdizajn/pen/bEdgLQ?editors=001

in place for that preventDefault call I used this peace of code:为了那个 preventDefault 调用,我使用了这个代码的和平:

$(event.target).css('top', $(event.target).data('startpos') )

in relation to:和---关联:

$(event.target).data('startpos', ui.position.top);

My idea was to track start/stop evt and to do some stuff there,, you can see and read my comments on codepen,, hope that can be of some help,我的想法是跟踪启动/停止 evt 并在那里做一些事情,你可以看到和阅读我对 codepen 的评论,希望能有所帮助,

cheers, k干杯,k

You need to change your if statement to this, and get rid of the preventDefault:您需要将 if 语句更改为此,并摆脱 preventDefault:

if ( draggingMin ) {
  if ( isMinLower ) {
    component.minPosition = ui.position.top;
  } else {
    ui.position.top = component.minPosition;
  }
} else if (draggingMax) {
  if( isMaxHigher ){
    component.maxPosition = ui.position.top;
  } else {
    ui.position.top = component.minPosition;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM