Range是HTML5中新出现的滑块控件,也是常见的控件的之一,不过这个控件的原始样式略丑,所以想对它进行一些改造。需要注意的是Internet Explorer 9及更早IE版本并不支持这个控件。
滑动条各浏览器原始样式

| 属性 | 描述 |
|---|---|
| max | 设置或返回滑块控件的最大值 |
| min | 设置或返回滑块控件的最小值 |
| step | 设置或返回每次拖动滑块控件时的递增量 |
| value | 设置或返回滑块控件的 value 属性值 |
| defaultValue | 设置或返回滑块控件的默认值 |
| autofocus | 设置或返回滑块控件在页面加载后是否应自动获取焦点 |
需要完成以下的五个步骤
- 去除系统默认的样式;
- 给滑动轨道(track)添加样式;
- 给滑块(thumb)添加样式;
- 根据滑块所在位置填充进度条;
- 实现多浏览器兼容。
最终运行效果


实现代码
<!DOCTYPE html>
<html>
<head>
<title>demo</title>
<script type="text/javascript" src="lib/jquery.js"></script>
<script type="text/javascript">
$.fn.RangeSlider = function(cfg){
this.sliderCfg = {
min: cfg && !isNaN(parseFloat(cfg.min)) ? Number(cfg.min) : null,
max: cfg && !isNaN(parseFloat(cfg.max)) ? Number(cfg.max) : null,
step: cfg && Number(cfg.step) ? cfg.step : 1,
callback: cfg && cfg.callback ? cfg.callback : null
};
var $input = $(this);
var min = this.sliderCfg.min;
var max = this.sliderCfg.max;
var step = this.sliderCfg.step;
var callback = this.sliderCfg.callback;
$input.attr('min', min)
.attr('max', max)
.attr('step', step);
$input.bind("input", function(e){
$input.attr('value', this.value);
$input.css( 'background', 'linear-gradient(to right, #059CFA, white ' + this.value + '%, white)' );
if ($.isFunction(callback)) {
callback(this);
}
});
};
</script>
<style type="text/css">
input[type=range] {
-webkit-appearance: none;
width: 300px;
border-radius: 10px; /*这个属性设置使填充进度条时的图形为圆角*/
background: -webkit-linear-gradient(#059CFA, #059CFA) no-repeat;
background-size: 0% 100%;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
}
input[type=range]::-webkit-slider-runnable-track {
height: 15px;
border-radius: 10px; /*将轨道设为圆角的*/
box-shadow: 0 1px 1px #def3f8, inset 0 .125em .125em #0d1112; /*轨道内置阴影效果*/
}
input[type=range]:focus {
outline: none;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
height: 25px;
width: 25px;
margin-top: -5px; /*使滑块超出轨道部分的偏移量相等*/
background: #ffffff;
border-radius: 50%; /*外观设置为圆形*/
border: solid 0.125em rgba(205, 224, 230, 0.5); /*设置边框*/
box-shadow: 0 .125em .125em #3b4547; /*添加底部阴影*/
}
input[type=range]::-moz-range-progress {
background: linear-gradient(to right, #059CFA, white 100%, white);
height: 13px;
border-radius: 10px;
}
input[type=range] {
-webkit-appearance: none;
width: 300px;
border-radius: 10px;
}
input[type=range]::-ms-track {
height: 25px;
border-radius: 10px;
box-shadow: 0 1px 1px #def3f8, inset 0 .125em .125em #0d1112;
border-color: transparent; /*去除原有边框*/
color: transparent; /*去除轨道内的竖线*/
}
input[type=range]::-ms-thumb {
border: solid 0.125em rgba(205, 224, 230, 0.5);
height: 25px;
width: 25px;
border-radius: 50%;
background: #ffffff;
margin-top: -5px;
box-shadow: 0 .125em .125em #3b4547;
}
input[type=range]::-ms-fill-lower {
/*进度条已填充的部分*/
height: 22px;
border-radius: 10px;
background: linear-gradient(to right, #059CFA, white 100%, white);
}
input[type=range]::-ms-fill-upper {
/*进度条未填充的部分*/
height: 22px;
border-radius: 10px;
background: #ffffff;
}
input[type=range]:focus::-ms-fill-lower {
background: linear-gradient(to right, #059CFA, white 100%, white);
background: linear-gradient(#059CFA, #059CFA) no-repeat;
}
input[type=range]:focus::-ms-fill-upper {
background: #ffffff;
background: linear-gradient(#059CFA, #059CFA) no-repeat;
}
</style>
</head>
<body>
<div id="test">
<input type="range" value="0">
</div>
<script>
var change = function($input) {
/*内容可自行定义*/
console.log("123");
}
$('input').RangeSlider({ min: 0, max: 100, step: 0.1, callback: change});
</script>
</body>
</html>
关于兼容IE9+浏览器的内容
针对IE浏览器可能出现css无法加载,导致我们样式没有体现出来的问题。之前的做法是将HTML第一行的 <!DOCTYPE html> 改为 <!DOCTYPE>,这种做法不是很好,后来在网上找到了更好地解决方案,详细内容可参考这个链接:http://www.uedsc.com/css-mime-type-mismatch.html这里简单说一下本地使用浏览器时的解决方案:
- 点击[win+R]键,在弹出的运行窗口输入“regedit”打开注册表编辑器;
- 找到 HKEY_LOCAL_MACHINE/SOFTWARE/Classes 下的 .css 项,确保 Content Type 为 text/css 即可。

在IE浏览器运行程序的时候,底部可能会提示你说已限制运行脚本,这会导致JS文件加载不了,点击“允许阻止的内容”即可。

源代码下载地址:http://download.csdn.net/download/u013347241/9742764
![HTML5 自定义input[type=”range”]滑动条样式](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
: » HTML5 自定义input[type=”range”]滑动条样式
原创文章,作者:wdmbts,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/251185.html