jQuery UI Range Slider - 单击滑块中间时移动右侧拖动器(手柄)
我按以下方式使用 jQueryUI 范围滑块:
$(function() {
$( "#slider-range" ).slider({
range: true,
min: 0,
max: 4,
step: 1,
values: [ 0, 4 ],
slide: function( event, ui ) {
$( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
}
});
$( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
" - $" + $( "#slider-range" ).slider( "values", 1 ) );
});
我需要做的是 - 当您单击滑块中间时,将右侧拖动器(手柄)移动到单击的位置。目前,当您单击中间时,左侧拖动器(手柄)将被拖动到该位置。有什么办法可以改变这种行为吗?
感谢您的帮助!
I'm using the jQueryUI range slider in the following way:
$(function() {
$( "#slider-range" ).slider({
range: true,
min: 0,
max: 4,
step: 1,
values: [ 0, 4 ],
slide: function( event, ui ) {
$( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
}
});
$( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
" - $" + $( "#slider-range" ).slider( "values", 1 ) );
});
What I need to do is - move the right side dragger (handle) to the clicked position when you click in the middle of the slider. Currently, when you click in the middle, the left dragger (handle) is being dragged to that position. Is there a way I can swap that behavior?
Thanks for helping!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上并不是这样的,尽管看起来可能是这样。它实际上会检测哪个手柄最接近,然后选择那个。每次单击时,都会计算距两个手柄的距离(以像素为单位)。更容易看到更多范围的功能,请参阅我制作的这个小提琴: http://jsfiddle.net/gm5nm/
这是 jQueryUI 使用的函数:
source: jquery.ui.slider.js第249行
总的来说,我认为这个是可用的。正确地覆盖它可能有点困难,因为它位于另一个包含一堆逻辑的方法内的匿名函数内。你可以,我真的真的不推荐它,但是你可以修改源代码并将最接近的句柄设置为第二个。但说真的,不要这样做。这是一个非常不好的习惯。
That's actually not how it works, although it may appear that way. It actually detects which handle is closest, and choose that one. Each time you click, the distance from BOTH handles, in pixels, is calculated. It's easier to see the functionality with MORE range, see this fiddle I made: http://jsfiddle.net/gm5nm/
Here is the function jQueryUI uses:
source: Line 249 of jquery.ui.slider.js
In general, I think this is usable. Overriding this properly may be a bit difficult because it's inside an anonymous function inside another method which contains a bunch of logic. You COULD, and I really really really don't recommend it, but you COULD modify the source and just set the closest handle to the 2nd. But seriously, don't do that. It's a very bad habbit.
我知道这是一个老问题,但我遇到了同样的问题并找到了一种更简单的方法来处理它。基本上,您需要禁用滑块本身的指针事件,在手柄上启用它们,然后将“单击”事件附加到手动移动滑块的容器 div。步骤如下:
假设这个 html:
CSS 会像这样:
你的 javascript 看起来像这样:
好处是你可以避免弄乱 jquery 代码。另外,手柄本身上的事件仍然正常运行。
缺点是您必须自己手动处理滑块单击操作。但如上所示,这并不难做到。
无论如何,希望这对遇到此问题的其他人有用。
I know this is an old question, but I was running into the same problem and figured out an easier way to handle it. Basically, you need to disable pointer events on the slider itself, enable them on the handles, and then attach a 'click' event to a container div that manually moves your slider. Here are the steps:
Assume this html:
The CSS would like this:
Your javascript would look like this:
The upside is you avoid messing with the jquery code. Plus, events on the handles themselves still function normally.
The downside is that you must handle the slider clicks yourself, manually. But as shown above, this isn't that hard to do.
Anyway, hope this is useful for others running into this problem.