思路:
首先既然是滑块拖动,那就必须要先把小滑块做出来。接着我们可以通过设置三条滑块,对应颜色中的rgb,然后通过js设置div的background-color就可以了。
而滑块拖动本质上来说就是div的拖动,话不多说直接上代码:
let line = 0; //确定鼠标按下,防止滑块在鼠标未按下时移走
let oldB = 0; //滑块的原位置
let oldM = 0; //鼠标的原位置
let oObj ; //获取滑块对象(原谅我起变量名字随意了一点)
//鼠标按下事件
function start(obj) {
oldB = parseInt(obj.style.marginLeft) //获取滑块的原位置
let e = event || window.event;
oldM = e.clientX //获取鼠标原位置
if(oldB >=0 && oldB <= 255) { //设定范围,超出这个范围是异常情况,不允许滑块移动
line = 1; //允许滑块移动
oObj = obj; //获取按下时滑块对象
}
}
//鼠标移动事件
function blockMove(){
if(line == 1) //当鼠标按下时允许滑动
{
let e = event || window.event;
let PX = oldB - (oldM - e.clientX) //获取鼠标偏移量
/*设定范围*/
if(PX > 255) PX = 255
if(PX < 0) PX = 0
oObj.style.marginLeft = PX +"px" //更改滑块位置,使其跟随鼠标移动
}
}
//鼠标松开事件
function blockEnd(){
/*不允许滑块移动,清除滑块对象*/
line = 0;
oObj = null;
}
加上div:
<div style="width:255px;height:5px;background-color: #bbbbbb;margin-top: 30px;margin-left: 20px" > <!--这是线,滑块在线上滑-->
<div style="width: 15px;height:20px;background-color: #f87272;margin-left: 0px" onmousedown="start(this)" onmousemove="blockMove()" onmouseup="blockEnd()"></div><!--这是滑块-->
</div>
这样,我们就做好了一个小滑块,然后直接Ctrl+c/v ,就做好了三个滑块。
但是我们发现滑块滑动的并不是很顺畅,一旦鼠标移动速度块一点,滑块就不跟随鼠标移动了,所以为了解决这个问题,我在body标签上直接加入了鼠标移动事件
<body onmousemove="blockMove()" onmouseup="blockEnd()">
实时监听鼠标移动(可能有更好的办法)。
不管怎么说,现在的滑块滑动的就十分顺畅了。
不过,滑块在滑动我们根本不知道具体数值,所以我们还要加上提示文本:
div部分:
<div style="width:255px;height:5px;background-color: #bbbbbb;margin-top: 30px;margin-left: 20px" ><!--这是线-->
<div id="r" style="width: 15px;height:20px;background-color: #f87272;margin-left: 0px" onmousedown="start(this)" onmousemove="blockMove()" onmouseup="blockEnd()"><!--这是滑块-->
<div style="float:left;margin-left: 15px" id="rT" >0</div><!--这是提示-->
</div>
</div>
<div style="width:255px;height:5px;background-color: #bbbbbb;margin-top: 30px;margin-left: 20px" >
<div id="g" style="width: 15px;height:20px;background-color: #72a877;margin-left: 0px" onmousedown="start(this)" onmousemove="blockMove()" onmouseup="blockEnd()">
<div style="float:left;margin-left: 15px" id="gT" >0</div>
</div>
</div>
<div style="width:255px;height:5px;background-color: #bbbbbb;margin-top: 30px;margin-left: 20px" >
<div id="b" style="width: 15px;height:20px;background-color: #60719f;margin-left: 0px" onmousedown="start(this)" onmousemove="blockMove()" onmouseup="blockEnd()">
<div style="float:left;margin-left: 15px" id="bT">0</div>
</div>
</div>
js:
//还是之前的鼠标移动事件
function blockMove(){
if(line == 1)
{
let e = event || window.event;
et PX = oldB - (oldM - e.clientX)
if(PX > 255) PX = 255
if(PX < 0) PX = 0
oObj.style.marginLeft = PX +"px"
//在这里加了一行更改提示div里面的文本,让文本内容是滑块的marginLeft的数值
oObj.children[0].innerText = parseInt(oObj.style.marginLeft)
}
}
现在滑块已经全部做好了,接下来就是和div图层的关联了
div图层:
<div id="main" style="position:absolute;left:100px;top:200px;width: 600px;height:400px;float:left;background-color: rgb(174,236,149) ;z-Index: 120; "></div>
js:
//还是那个鼠标移动事件(我不是为了水文章)
function blockMove(){
if(line == 1)
{
let e = event || window.event;
let PX = oldB - (oldM - e.clientX)
if(PX > 255) PX = 255
if(PX < 0) PX = 0
oObj.style.marginLeft = PX +"px"
oObj.children[0].innerText = parseInt(oObj.style.marginLeft)
//获取提示文本,三个提示文本分别代表r,g,b
let r = document.getElementById("rT").innerText
let g = document.getElementById("gT").innerText
let b = document.getElementById("bT").innerText
//设定背景颜色
document.getElementById("main").style.backgroundColor = "rgb("+r+","+g+","+b+")"
}
}
现在我们已经能通过滑动滑块改变div的背景颜色了,但是还有一点点瑕疵是我一开始已经设置了div的rgb,但是滑块仍然从最左边开始,所以我又加了初始化。
//获取div背景颜色
let opennessFirst = document.getElementById("main").style.backgroundColor.split(',')
let Or = parseInt(opennessFirst[0].substring(4))
let Og = parseInt(opennessFirst[1])
let Ob = parseInt(opennessFirst[2])
document.getElementById("r").style.marginLeft = Or + 'px'; document.getElementById("rT").innerText = Or;
document.getElementById("g").style.marginLeft = Og + 'px'; document.getElementById("gT").innerText = Og;
document.getElementById("b").style.marginLeft = Ob + 'px'; document.getElementById("bT").innerText = Ob;
下面是完整代码:
<body onmousemove="blockMove()" onmouseup="blockEnd()">
<div id="main" style="width: 600px;height:400px;float:left;background-color: rgb(174,236,149) ;z-Index: 120; "></div>
<div id="Color" style="position: absolute;top:600px;z-index:901;height: 140px;width: 300px;"><!--给三个滑块套了一个盒子-->
<div style="width:255px;height:5px;background-color: #bbbbbb;margin-top: 30px;margin-left: 20px" >
<div id="r" style="width: 15px;height:20px;background-color: #f87272;margin-left: 0px" onmousedown="start(this)" onmousemove="blockMove()" onmouseup="blockEnd()">
<div style="float:left;margin-left: 15px" id="rT" >0</div>
</div>
</div>
<div style="width:255px;height:5px;background-color: #bbbbbb;margin-top: 30px;margin-left: 20px" >
<div id="g" style="width: 15px;height:20px;background-color: #72a877;margin-left: 0px" onmousedown="start(this)" onmousemove="blockMove()" onmouseup="blockEnd()">
<div style="float:left;margin-left: 15px" id="gT" >0</div>
</div>
</div>
<div style="width:255px;height:5px;background-color: #bbbbbb;margin-top: 30px;margin-left: 20px" >
<div id="b" style="width: 15px;height:20px;background-color: #60719f;margin-left: 0px" onmousedown="start(this)" onmousemove="blockMove()" onmouseup="blockEnd()">
<div style="float:left;margin-left: 15px" id="bT">0</div>
</div>
</div>
<script>
let line = 0; //确定鼠标按下,防止滑块在鼠标未按下时移走
let oldB = 0; //滑块的原位置
let oldM = 0; //鼠标的原位置
let oObj ; //获取滑块对象(原谅我起变量名字随意了一点)
//获取div背景颜色
let opennessFirst = document.getElementById("main").style.backgroundColor.split(',')
let Or = parseInt(opennessFirst[0].substring(4))
let Og = parseInt(opennessFirst[1])
let Ob = parseInt(opennessFirst[2])
document.getElementById("r").style.marginLeft = Or + 'px'; document.getElementById("rT").innerText = Or;
document.getElementById("g").style.marginLeft = Og + 'px'; document.getElementById("gT").innerText = Og;
document.getElementById("b").style.marginLeft = Ob + 'px'; document.getElementById("bT").innerText = Ob;
//鼠标按下事件
function start(obj) {
oldB = parseInt(obj.style.marginLeft) //获取滑块的原位置
let e = event || window.event;
oldM = e.clientX //获取鼠标原位置
if(oldB >=0 && oldB <= 255) { //设定范围,超出这个范围是异常情况,不允许滑块移动
line = 1; //允许滑块移动
oObj = obj; //获取按下时滑块对象
}
}
//鼠标移动事件
function blockMove(){
if(line == 1) //当鼠标按下时允许滑动
{
let e = event || window.event;
let PX = oldB - (oldM - e.clientX) //获取鼠标偏移量
/*设定范围*/
if(PX > 255) PX = 255
if(PX < 0) PX = 0
oObj.style.marginLeft = PX +"px" //更改滑块位置,使其跟随鼠标移动
//在这里加了一行更改提示div里面的文本,让文本内容是滑块的marginLeft的数值
oObj.children[0].innerText = parseInt(oObj.style.marginLeft)
//获取提示文本,三个提示文本分别代表r,g,b
let r = document.getElementById("rT").innerText
let g = document.getElementById("gT").innerText
let b = document.getElementById("bT").innerText
//设定背景颜色
document.getElementById("main").style.backgroundColor = "rgb("+r+","+g+","+b+")"
}
}
//鼠标松开事件
function blockEnd(){
/*不允许滑块移动,清除滑块对象*/
line = 0;
oObj = null;
}
</script>
</body>
下面附上效果图
这是刚加载网页
随便滑动了几下