移动端滑动框(Slider)是一种常见的用户界面组件,允许用户通过滑动手指来选择一个值或浏览一系列内容。滑动框通常用于调整设置、浏览图片或视频、选择日期等场景。
以下是一个简单的JavaScript实现移动端滑动框的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>移动端滑动框</title>
<style>
.slider-container {
width: 100%;
padding: 20px;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider:hover {
opacity: 1;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
</style>
</head>
<body>
<div class="slider-container">
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
</div>
<script>
const slider = document.getElementById('myRange');
const output = document.createElement('p');
output.innerHTML = `Value: ${slider.value}`;
document.body.appendChild(output);
slider.oninput = function() {
output.innerHTML = `Value: ${this.value}`;
}
</script>
</body>
</html>
通过以上示例和解决方案,你可以实现一个基本的移动端滑动框,并解决常见的使用问题。
领取专属 10元无门槛券
手把手带您无忧上云