JS实现百分⽐⽔平条<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {margin:0;padding:0;}
.scroll {
width: 400px;
height: 8px;
background-color: #ccc;
margin: 100px;
position: relative;
border-radius: 4px;
}
.bar {
width: 10px;
height: 22px;
background-color: #369;
position: absolute;
top: -7px;
left: 0;
cursor: pointer;
border-radius: 4px;
}
.mask{
width: 0;
height: 100%;
background: #369;
border-radius: 4px 0 0 4px;
}
</style>
</head>
<body>
<div class="scroll" id="scroll">
<div class="bar"></div>
<div class="mask"></div>
</div>
<div id="demo"></div>
</body>
</html>
<script>
var scroll = ElementById("scroll");
var bar = scroll.children[0];
var mask = scroll.children[1];
var demo = ElementById("demo");
var that = this;
var event = event || window.event;
var left = event.clientX - this.offsetLeft;
var event = event || window.event;
that.style.left = event.clientX - left + "px";
var val = parseInt(that.style.left);
if(val < 0){
that.style.left = 0;
} else if(val > 390){
that.style.left = "390px";
}
margin属性值可以为百分比mask.style.width = that.style.left;
demo.innerHTML = parseInt(parseInt(that.style.left)/390*100)+"%";
}
}
}
</script>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论