jQuery 用户界面;停止传播可选事件
基本上我在 ul
上使用 jQuery ui 的可选 功能,但是ul 通常会有一个滚动条,并且该滚动条在 Webkit 浏览器中变得不可用,因为当您尝试单击它来抓取它时,可选功能的套索会被绘制在顶部。
我制定了一个解决方案,其中涉及检查光标相对于 ul 的位置和宽度的位置,以查看光标是否位于滚动条上方,如果是,则停止可选“开始”事件的传播,但尽管如此条件在应该满足的时候得到满足,既不返回 false 也不停止事件的传播似乎阻止 jQuery 处理可选事件。
以下是我对 jQuery .selectable
start
事件的了解:
start: function(event, ui) {
var t = event.target;
var cutoff = t.scrollWidth + t.offsetLeft
if (event.clientX > cutoff)
{
console.log('NO!');
console.log(event.type);
//overkill
event.stopPropagation();
event.stopImmediatePropagation();
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
return false;
}
}
所有建议/解决方案均表示赞赏。
Basically I am using jQuery ui's selectable functionality on a ul
, but the ul will often times have a scrollbar, and this scrollbar becomes unusable in Webkit browsers since when you try to click on it to grab it, the lasso for the selectable functionality is drawn overtop instead.
I have formulated a solution, which involves checking the position of the cursor in relation to the position and width of the ul to see if the cursor is over the scrollbar, and if so, stop propagation of the selectable 'start' event, but despite the conditional being met when it should be, neither returning false nor stopping progation of the event seems to prevent jQuery from progressing through the selectable events.
Here is what I have for the jQuery .selectable
start
event:
start: function(event, ui) {
var t = event.target;
var cutoff = t.scrollWidth + t.offsetLeft
if (event.clientX > cutoff)
{
console.log('NO!');
console.log(event.type);
//overkill
event.stopPropagation();
event.stopImmediatePropagation();
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
return false;
}
}
All advice/solutions appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
start
事件是一个棘手的人造事件。您需要做的是将取消事件冒泡的代码直接附加到ul
本身的mousedown
事件,并确保首先执行您的事件处理程序。您将在
event.stopPropagation
的 jQuery 文档中看到这一小行:因此,虽然
event.stopPropagation
将阻止事件在 DOM 上进一步冒泡,但它不会阻止附加到被调用的ul
的其他事件处理程序。为此,您需要event.stopImmediatePropagation
来停止调用selectable
事件处理程序。基于可选择演示页面,此代码片段成功取消了气泡:
请注意,您必须添加事件在执行
.selectable()
设置函数之前,将处理程序添加到 ul 对象,以便首先潜入并弹出事件气泡。The
start
event is a tricksy faux one. What you need to do is attach your code to cancel event bubbling directly to themousedown
event of theul
itself, and make sure your event handler is executed first.You'll see in the jQuery docs for
event.stopPropagation
this little line:So, whilst
event.stopPropagation
will stop the event bubbling any further up the DOM, it won't stop the other event handlers attach to theul
being called. For that you needevent.stopImmediatePropagation
to stop theselectable
event handler getting called.Based on the selectable demo page, this code snippet successfully cancels the bubble:
Note that you must add your event handler to the
ul
object before you execute the.selectable()
setup function in order to sneak in and pop the event bubble first.这是 Sam C 的解决方案的一个细微变化,它对我来说效果更好(仅取消 mousedown 事件,如果它在带有滚动条的元素上触发):
Here's a slight variation on Sam C's solution that worked better for me (by only cancelling the mousedown event if it's fired on the element with the scrollbar):
Sam C 的答案对我不起作用,可能是因为我定位 #selectable 的方式。这就是我使用的:
其中 $.getScrollbarWidth() 来自 这里
Sam C's answer didn't work for me, probably because of the way I'd positioned #selectable. This is what I used:
where $.getScrollbarWidth() is from here