滑动条
Posted mmit
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了滑动条相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>滑动条</title>
<style type="text/css">
.box
width: 400px;
height: 8px;
background-color: #ccc;
margin: 100px auto;
position: relative;
.box .bar
width: 10px;
height: 20px;
background-color: #369;
position: absolute;
top: -6px;
left: 0;
cursor: pointer;
.box .mask
width: 0px;
height: 100%;
background-color: #369;
</style>
</head>
<body>
<div class="box" id="box">
<div class="bar" id="bar"></div>
<div class="mask" id="mask"></div>
</div>
<script type="text/javascript">
function $(id)
return document.getElementById(id);
var bar = $(‘bar‘);
//onclick事件,是鼠标点击下去,然后松开手才会触发
//onmousedown事件,是鼠标点击下去就触发
bar.onmousedown = function(event)
var evt = event || window.event;
var mLeft = evt.clientX - bar.offsetLeft;
document.onmousemove = function(event)
var evt = event || window.event;
var barX = evt.clientX - mLeft;
if(barX < 0)
barX = 0;
if(barX > (bar.parentNode.offsetWidth - bar.offsetWidth))
barX = (bar.parentNode.offsetWidth - bar.offsetWidth)
bar.style.left = barX + ‘px‘;
$(‘mask‘).style.width = barX + ‘px‘;
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
//一定要在bar.onmousedown内部注册,否则不起作用
document.onmouseup = function()
document.onmousemove = null;
</script>
</body>
</html>
以上是关于滑动条的主要内容,如果未能解决你的问题,请参考以下文章