javascript - 如何让 slider 长度变短并出现在页面中央?

标签 javascript html css slider

<分区>

现在我有一个从 0 到 100 的 slider 。但是,它出现在页面顶部并占据了页面的整个宽度。我想让 slider 的长度变短并在页面上居中。谢谢!

这是我目前所拥有的:https://jsfiddle.net/codebeginner1/vd5uLqgz/2/

<meta name="viewport" content="width=device-width, initial-scale=1">

最佳答案

只需使用 CSS 来设置样式:

$( function() {
    var handle = $( "#custom-handle" );
    $( "#slider" ).slider({
      create: function() {
        handle.text( $( this ).slider( "value" ) );
      },
      slide: function( event, ui ) {
        handle.text( ui.value );
      }
    });
  } );
#custom-handle {
    width: 3em;
    height: 1.6em;
    top: 50%;
    margin-top: -.8em;
    text-align: center;
    line-height: 1.6em;
  }
  

  /* Set the slider width to whatever you want and then
     cause the div to be centered by setting both margins to auto. */
  #slider { width:200px; margin:auto; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Slider - Custom handle</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  </head>
<body>
 
<div id="slider">
  <div id="custom-handle" class="ui-slider-handle"></div>
</div>
 
 
</body>
</html>

关于javascript - 如何让 slider 长度变短并出现在页面中央?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57205974/

相关文章:

php - 在 php 中请求选择选项

javascript - 如何使用 React Spring 连接一个元素上的两个动画?

用于服务器端验证的 javascript 事务日志

html - 塌陷时不均匀的 Bootstrap 药丸

html - 使用 BEM CSS 制作 slider

javascript - 使用express Nodejs multer上传文件?

javascript - AngularJS 自定义提供程序导致 "Unknown provider"异常

jquery - 单击我的 webapp 中的 url 链接下载页面,有时单击被禁用可能是什么原因?

javascript - jQuery区分一个元素和一类元素

html - 一个标签在 chrome 中分成两行,为什么?