如何更改Jquery UI Slider句柄

Tom*_*Tom 43 jquery jquery-ui slider jquery-plugins

我想修改库存JQuery UI滑块,以便手柄上有一个箭头而不是正方形.即我想使用自定义图像作为句柄.

有一些教程可以做到:

但我无法让它发挥作用.以下代码生成固定句柄图像:

<!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>
Run Code Online (Sandbox Code Playgroud)

就好像JQuery没有拿起我想要使用myhandle id作为句柄.我想知道:我是否需要一个JQuery插件来识别句柄选项?(它没有记录在http://docs.jquery.com/UI/Slider中).或许它只能在旧版本的JQuery中运行?

有任何想法吗?

Tom*_*Tom 48

可以更改为将图像添加到JQuery滑块手柄的CSS类称为" .ui-slider-horizo​​ntal .ui-slider-handle ".

以下代码显示了一个演示:

<!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>
Run Code Online (Sandbox Code Playgroud)

我认为注册一个句柄选项是旧的方式,在JQuery-ui 1.7.2中不再支持?

  • 我在DW中复制并粘贴了这个确切的代码并尝试了它.我用自己的图像替换了图像.根本不是滑块或图像显示.我已经尝试了有关此主题的所有解决方案,但没有任何方法可以更改图像.我将在我自己的线程下发布我的代码,如果在SO上有一个可行的解决方案,希望有人可以在SO上列出URL. (3认同)
  • png的链接被破坏了. (2认同)

And*_*tts 18

.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; 
}
Run Code Online (Sandbox Code Playgroud)


apa*_*aul 15

如果你需要完全用其他东西替换手柄,而不仅仅是重新安装它:

您可以通过创建和附加元素并在初始化之前添加ui-slider-handle类来指定自定义句柄元素.

工作实例

$('.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
});
Run Code Online (Sandbox Code Playgroud)
.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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)