Jquery UI滑块如何使一个框跟随处理程序?
我正在尝试创建一个应遵循处理程序的 div,如下所示: http://m1.dk/Priser/#tab:#calc,#abb< /a>
我的 JSfiddle: http://jsfiddle.net/z3xV3/2/
我还想对应该出现在框中的 UI 值进行计算。
这是我想要实现的目标的说明:
HTML:
<div id="slider"></div>
<input id="sliderValue" />
Jquery:
$(document).ready(function() {
// Pris slider
$("#slider").slider({value:'',min: 0,max: 150,step: 1, range: 'min',
slide: function( event, ui ) {
$( "#amount" ).html( ui.value + ' timer');
}
});
$( "#amount" ).val( "$" + $( "#slider" ).slider( "value" ) );
});
I am trying to create a div that should follow the handler as it is seen here:
http://m1.dk/Priser/#tab:#calc,#abb
My JSfiddle: http://jsfiddle.net/z3xV3/2/
I also want to make a calculation on the UI value that should appear in the box.
Here is a illustration what I want to achieve:
HTML:
<div id="slider"></div>
<input id="sliderValue" />
Jquery:
$(document).ready(function() {
// Pris slider
$("#slider").slider({value:'',min: 0,max: 150,step: 1, range: 'min',
slide: function( event, ui ) {
$( "#amount" ).html( ui.value + ' timer');
}
});
$( "#amount" ).val( "$" + $( "#slider" ).slider( "value" ) );
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我希望这就是您正在寻找的效果: http://jsfiddle.net/z3xV3/11/
JS
HTML
CSS
最诚挚的问候!
I hope that's the effect you're looking for: http://jsfiddle.net/z3xV3/11/
JS
HTML
CSS
Best regards!
我修改了你的jsfiddle示例:http://jsfiddle.net/Dr5UR/
我利用了滑块。
幻灯片事件有两个参数:
事件
该对象包含有关事件本身的多个信息。触发的时间、触发时刻按下的按键或者鼠标的坐标。 clientX 属性包含我们需要设置的移动对象的位置。
用户界面
ui 对象来自 jQuery UI 本身。它基本上只包含值。此外,它还包含用于拖动的锚点。但你不能使用它的位置,因为滑动事件在锚点到达新位置之前生效。
#slideValue
输入需要具有position:absolute;
,因为我使用 CSS 中的 left 位置属性。您还可以设置margin-left
值,而无需将position
设置为absolute
。I have modifiered your jsfiddle example: http://jsfiddle.net/Dr5UR/
I make use of the slide event of the slider.
The slide event got two arguments:
event
This object contains several informations about the event itself. The time of triggering, the key which has been pressed or the coordinates of the mouse at the moment of triggering. The clientX property contains the position we need to set to the moving object.
ui
The ui object comes from jQuery UI itself. It basicly just contains the value. Additionally it contains the anchor which is used for dragging too. But you can't use its position, cause the slide event takes effect before the anchor takes its new position.
The
#slideValue
input needs to haveposition: absolute;
, cause I work with the left position attribute in CSS. You could also set themargin-left
value, without need to set theposition
toabsolute
.将其变成一个简单的 jQuery 插件实际上非常容易(而且非常有用)。像这样:
JSFiddle: http://jsfiddle.net/PPvG/huYRL/请注意,我通过在#sliderValue
中输入值来添加对移动滑块的支持。[ 代码示例已删除 ]
您也可以选择删除来自 HTML 的#sliderValue
并让插件为您创建它。在这种情况下,您将重写该插件,以便在滑块上调用它,而不是在#sliderValue
上调用。在我看来,这将是一个更简洁的解决方案。更新
根据您的评论(并重新阅读您的问题),我重写了插件示例。
如果我理解正确的话,您想要根据滑块的值进行一些计算,并将这些计算的结果显示在滑块手柄下方的标签中。下面的内容应该会让这变得非常容易。
JSFiddle: http://jsfiddle.net/PPvG/q5qg7/
HTML
插件
用法
如您所见,现在在滑块本身上调用该插件。您现在可以为插件提供回调,它允许您执行一些计算并返回格式正确的标签(允许 HTML)。
在上面的示例中,回调仅采用传递给回调的
值
并附加'%'
。It's actually quite easy (and very useful) to turn this into a simple jQuery plugin. Like so:
JSFiddle: http://jsfiddle.net/PPvG/huYRL/Note that I've added support for moving the slider by entering a value into the#sliderValue
.[ Code sample removed ]
You could also choose to remove#sliderValue
from the HTML and let the plugin create it for you. In that case, you would rewrite the plugin so it would be called on the slider, instead of on#sliderValue
. In my opinion, that would be a much neater solution.Update
Based on your comments (and re-reading your question), I've rewritten the plugin example.
If I understand correctly, you want to do some calculations based on the value of the slider and display the result of those calculations in the label below the slider handle. The following should make that really easy.
JSFiddle: http://jsfiddle.net/PPvG/q5qg7/
HTML
Plugin
Usage
As you can see, the plugin is now called on the slider itself. You can now provide the plugin with a callback, which allows you to do some calculations and return a correctly formatted label (HTML is allowed).
In the example above, the callback simply takes the
value
that is passed to the callbackand appends'%'
.这是一个带有两个手柄的滑块的工作示例
CSS:
Here is a working example for a slider with two handles
CSS:
HTML:
CSS:
jQuery:
http://jsfiddle.net/yBfTy/4/
HTML:
CSS:
jQuery:
http://jsfiddle.net/yBfTy/4/
最好看看带有 jQuery 插件的替代解决方案,例如:
http://craigsworks.com/projects/qtip2/demos/#ui-slider
http://www.filamentgroup.com/lab/update_jquery_ui_slider_from_a_select_element_now_with_aria_support/
It would be nice to take a look at alternative solutions with jQuery plugins such as:
http://craigsworks.com/projects/qtip2/demos/#ui-slider
http://www.filamentgroup.com/lab/update_jquery_ui_slider_from_a_select_element_now_with_aria_support/
带上你的小提琴。
HTML:
CSS:
#sliderValue{width:50px;}
Jquery:
Taking your jsfiddle.
HTML:
CSS:
#sliderValue{width:50px;}
Jquery:
您可以: http://jsfiddle.net/z3xV3/21/
幻灯片回调被调用移动手柄时:
offsetLeft
是手柄的偏移量。$( "#sliderValue" )
是您的输入。You can just: http://jsfiddle.net/z3xV3/21/
The slide callback is called every time you move your handle:
offsetLeft
is the offset of the handle.$( "#sliderValue" )
is your input.