如何更改 Jquery UI 滑块句柄
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1207307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Jquery UI Slider handle
提问by Tom
I want to modify the stock JQuery UI slider so that the handle has a arrow on it rather than being a square. i.e. I want to use a custom image as the handle.
我想修改股票的 JQuery UI 滑块,以便手柄上有一个箭头而不是一个正方形。即我想使用自定义图像作为句柄。
There are a few tutorials that do it:
有一些教程可以做到这一点:
- http://jqueryfordesigners.com/slider-gallery/
- http://www.ryancoughlin.com/2008/11/04/using-the-jquery-ui-slider/
- http://www.keepthewebweird.com/creating-a-nice-slider-with-jquery-ui/
- http://jqueryfordesigners.com/slider-gallery/
- http://www.ryancoughlin.com/2008/11/04/using-the-jquery-ui-slider/
- http://www.keepthewebweird.com/creating-a-nice-slider-with-jquery-ui/
But I can't get it to work. The following code results in a stationary handle image:
但我无法让它工作。以下代码产生一个固定的手柄图像:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"></script>
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.slider.js"></script>
<style type="text/css">
#myhandle {position: absolute;z-index: 100;height: 25px;width: 35px;top: auto;background: url(http://stackoverflow.com/content/img/so/vote-arrow-down.png) no-repeat;}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("#slider").slider({handle: '#myhandle'});
});
</script>
</head>
<body>
<div id="slider"><div id="myhandle"></div></div>
</body>
</html>
It is as if JQuery doesn't pick up that I want to use the myhandleid for the handle. I'm wondering: Do I need a plugin for JQuery to recognise the handleoption? (it is not documented in http://docs.jquery.com/UI/Slider). Or perhaps it only worked in an old version of JQuery?
就好像 JQuery 没有发现我想使用myhandleid 作为句柄。我想知道:我需要一个 JQuery 插件来识别句柄选项吗?(它没有记录在http://docs.jquery.com/UI/Slider 中)。或者它可能只适用于旧版本的 JQuery?
Any ideas?
有任何想法吗?
回答by Tom
The CSS class that can be changed to add a image to the JQuery slider handle is called ".ui-slider-horizontal .ui-slider-handle".
可以更改以将图像添加到 JQuery 滑块句柄的 CSS 类称为“ .ui-slider-horizontal .ui-slider-handle”。
The following code shows a demo:
以下代码显示了一个演示:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"></script>
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.slider.js"></script>
<style type="text/css">
.ui-slider-horizontal .ui-state-default {background: white url(http://stackoverflow.com/content/img/so/vote-arrow-down.png) no-repeat scroll 50% 50%;}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("#slider").slider();
});
</script>
</head>
<body>
<div id="slider"></div>
</body>
</html>
I think registering a handleoption was the old way of doing it and no longer supported in JQuery-ui 1.7.2?
我认为注册句柄选项是旧的方式,在 JQuery-ui 1.7.2 中不再支持?
回答by Andrew Betts
.ui-slider .ui-slider-handle{
width:50px;
height:50px;
background:url(../images/slider_grabber.png) no-repeat; overflow: hidden;
position:absolute;
top: -10px;
border-style:none;
}
回答by apaul
If you should need to replace the handle with something else entirely, rather than just restyling it:
如果您需要完全用其他东西替换手柄,而不仅仅是重新设计它:
$('.slider').append('<div class="my-handle ui-slider-handle"><svg height="18" width="14"><path d="M13,9 5,1 A 10,10 0, 0, 0, 5,17z"/></svg></div>');
$('.slider').slider({
range: "min",
value: 10
});
.slider .ui-state-default {
background: none;
}
.slider.ui-slider .ui-slider-handle {
width: 14px;
height: 18px;
margin-left: -5px;
top: -4px;
border: none;
background: none;
}
.slider {
height: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<div class="slider"></div>
回答by Xhanch Studio
You also should set border:none
to that css class.
您还应该设置border:none
为该 css 类。
回答by anatoli
This change only first handle in multihandle slider. In apiDoc you can see:"For example, if you specify values: [ 1, 5, 18 ] and create one custom handle, the plugin will create the other two."
此更改仅在多手柄滑块中的第一个手柄。在 apiDoc 中,您可以看到:“例如,如果您指定值:[ 1, 5, 18 ] 并创建一个自定义句柄,插件将创建另外两个。”