向 jQuery UI 滑块添加填充
有没有一种简单的方法可以向 jQuery UI Slider 的滑块添加填充?我希望滑块手柄不会到达杆的末端。因此,如果这是常规的左端:
no padding
\/
[]------------------
我希望它在左(和右)端有填充:
padding of 10px for instance
\/
--[]----------------
我在 css 文件中尝试了此操作:
.ui-slider-horizontal { height: .8em;内边距:0 10px; 。
但似乎 jQuery UI Slider 使用完整的 ( outerWidth()
) 宽度来确定手柄的滑动范围,而不是没有填充的宽度 我还尝试破解代码,将 outerWidth()
替换为 width()
,但它没有做任何事情。
我明确不想诉诸设置最小值和最大值的技巧。这些值应始终按预期发挥作用。
有什么建议吗?
编辑:
遵循pkauko的评论;我使用滑块作为一个元素,上面有三个标签:
not very
important important important
----[]-----------[]----------[]----
上图指示了我希望手柄出现的位置,位于标签下方的中心。但该条在视觉上需要拉伸到边缘。
Is there an easy way to add padding to the slider-bar of a jQuery UI Slider? I'ld like to be able to have the slider-handle not reach the ends of the bar. So if this is the regular left end:
no padding
\/
[]------------------
I would like it to have padding on the left (and right) end:
padding of 10px for instance
\/
--[]----------------
I tried this in the css file:
.ui-slider-horizontal { height: .8em; padding: 0 10px; }
But it seems jQuery UI Slider uses the the complete ( outerWidth()
) width to determine the sliding range of the handle, instead of the width without padding. I also tried hacking the code, replacing outerWidth()
with width()
, but it didn't do anything.
I explicitly don't want to resort to a hack of setting min and max values. The values should always keep working as expected.
Any suggestions?
Edit:
Following pkauko's comment; I am using the slider as an element that has three labels above it:
not very
important important important
----[]-----------[]----------[]----
The above illustration indicates the positions where I want the handle to appear, centered underneath the labels. But the bar visually needs to stretch to the edges.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我自己也经常想要这样,但从来没有动力真正坐下来等等。
既然你现在问了,我确实坐下来修改了 jQuery UI 的滑块以根据你的要求添加填充选项。
注意:我只为带有单个手柄的水平滑块添加了它。其他情况并不复杂。我只是没有动力进行打字和后续测试来覆盖现在可能不需要的案例。
在包含 jQuery UI 中的原始 Slider 后,包含此代码。该扩展覆盖了几个方法,并将选项“paddingMin”和“paddingMax”添加到滑块小部件(值必须是数字,并将被解释为像素;显然,这也可以轻松扩展以采用其他单位,但同样将意味着对当前未请求的情况进行更多的打字/测试)。
简单示例用法:
扩展黑客代码(按原样提供,不要起诉我):
Ok, I've often wanted that myself, but never had the motivation actually to sit down and so something about it.
Since you asked now, I did sit down and hacked jQuery UI's slider to add the padding options as you requested.
Note: I've only added it for horizontal sliders with a single handle. The other cases aren't any more complicated. I just didn't have the motivation to do the typing and subsequent testing to cover cases that probably weren't needed right now.
Include this code after you've included the original Slider from jQuery UI. The extension overwrites a couple of methods and adds the options 'paddingMin' and 'paddingMax' to the Slider widget (values have to be numerical and will be interpreted as pixels; this too can be easily extended to take other units, obviously, but again would imply more typing/testing for currently unrequested cases).
Simple example usage:
Extension hack code (provided as is, don't sue me):
“捕捉增量”与“不兼容”(捕捉值不均匀)最小值和最大值相结合是否会产生所需的效果?我知道这很可能会导致应用程序端值转换,因为这可能无法使用现在使用的值来获得,但如果它有效,这里有一个“黑客和斜杠”“解决方案”。
我建议将最小值设置为 750。将捕捉值设置为 1200,将最大值设置为 4050。这样,最低捕捉值比最小值高 450,并且滑块永远不会到达最左边缘。最大值相同,即 3 x 1200 + 450 = 4050。此处使用的值是任意的,仅作为示例。
还没有测试过这个,也不知道它是否有效,我知道这不是您想要的,而只是当我在 jqueryui.com 检查 jquery UI 滑块如何工作的示例时想到的一个想法。
编辑:无法放弃这个,现在我知道它的工作原理是防止滑块到达幻灯片的末端。您只需相应地设置初始值,以防止页面加载时滑块位于一端或另一端。
Would the "snap to increments" coupled with "incompatible" (uneven to the snap values) min and max values give the desired effect? I know that this would more than likely lead to application side value conversion as this probably cannot be obtained using the values used now, but here's a "hack and slash" 'solution' if it works.
What I'm suggesting is set the min value to, say, 750. Snap value to 1200 and max value to 4050. That way the lowest snap is 450 higher than the minimum value and the slider should never reach the leftmost edge. Same for the max value, that's 3 x 1200 + 450 = 4050. The values used here are arbitrary and for example only.
Haven't tested this and have no idea if it works and I know this is not what you were looking for, but just an idea that came to mind, when I checked the examples of how jquery UI slider works at jqueryui.com.
Edit: couldn't let go of this and now I know this works in the way that it prevents the slider from reaching the ends of the slide. You just need to set the initial value accordingly to prevent the slider from being in one end or the other when the page loads.