<分区>
现在我有一个从 0 到 100 的 slider 。但是,它出现在页面顶部并占据了页面的整个宽度。我想让 slider 的长度变短并在页面上居中。谢谢!
这是我目前所拥有的:https://jsfiddle.net/codebeginner1/vd5uLqgz/2/
<meta name="viewport" content="width=device-width, initial-scale=1">
标签 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/