简体   繁体   English

除拖动时的敲除单击绑定

[英]Knockout Click Binding Except When Dragging

I have sliders on my site that allow the user to select via dragging, or clicking on an item. 我的网站上有一些滑块,允许用户通过拖动或单击项目进行选择。 When they drag, the item closest to the vertical center of the parent is chosen. 拖动时,将选择最接近父级垂直中心的项目。

CodePen Demo CodePen演示

The problem is that the Knockout click binding is being bound before the jQuery UI draggable option (implemented in a custom binding). 问题是在jQuery UI的可拖动选项(在自定义绑定中实现)之前绑定了Knockout点击绑定。

To reproduce, try clicking and dragging on the bottom number, and move it up a little (but not in the center). 若要重现,请尝试单击并拖动底部的数字,然后将其向上移动一点(但不要在中间)。 Try doing that a few times, as it seems to work correctly on the first drag... sometimes. 尝试几次,因为它似乎在第一次拖动时可以正常工作……有时。

The binding basically calls .draggable , and updates an observable when dragging stops. 绑定基本上调用.draggable ,并在拖动停止时更新一个observable。

The HTML is set up like this. 这样设置HTML。

<ul data-bind="slideSelect: thing, foreach: nums">
  <li data-bind="click: $root.thing.set($data), text: $data">1</li>
</ul>

Is there any way to make the draggable element stop the li s click events from firing? 有没有什么办法让拖动元素停止li烧制而成的Click事件? I'd like to do it in the cleanest fashion possible. 我想以最简洁的方式来做。

did you try event.preventDefault(); 您是否尝试过event.preventDefault(); in on drag event handler? 在拖动事件处理程序中? the idea is if you detect drag event then stop the event bubbling. 这个想法是,如果您检测到拖动事件,则停止事件冒泡。 if click happens before drag then i think you might have to wait to see if drag happens then complete the action. 如果单击发生在拖动之前,那么我想您可能必须等待以查看拖动是否发生,然后完成操作。

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

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