鼠标事件-拖拽(滑块控制物体透明度变化)

Posted 梁大爷

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鼠标事件-拖拽(滑块控制物体透明度变化)相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
#parent{
width: 600px;
height: 20px;
border: 1px black solid;
position: relative;
margin: auto;
}
#child{
width: 20px;
height: 20px;
background: red;
position: absolute;
top: 0px;
left: 0px;
}
#div{
width: 200px;
height: 200px;
background: goldenrod;
filter:alpha(opacity:0);
opacity: 0.0;
}
</style>
<script>
window.onload = function(){
var parent = document.getElementById(‘parent‘);
var child = document.getElementById(‘child‘);
var mydiv = document.getElementById(‘div‘);

//x,y分别代表鼠标与移动物体的距离(在移动过程中,这个距离始终保持不变)
var x = 0;
var y = 0;

document.onmousedown = function(ev){
var oEvent = ev || event;

x = oEvent.clientX - child.offsetLeft;

document.onmousemove = function(ev){
var oEvent = ev || event;
//m.n分别代表移动物体的横纵坐标
var m = oEvent.clientX - x;
var scale = (child.offsetLeft)/(parent.offsetWidth - child.offsetWidth);

if(m < 0){
m = 0;
}else if(m > (parent.offsetWidth - child.offsetWidth)){
m = parent.offsetWidth - child.offsetWidth;
}
child.style.left = m + ‘px‘;

mydiv.style.filter = ‘alpha(opacity:‘+scale*100+‘)‘;
mydiv.style.opacity = scale;

document.title = scale;
}

document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}

return false;
}

}
</script>
</head>
<body>
<div id="parent">
<div id="child"></div>
</div>
<div id="div"></div>
</body>
</html>

以上是关于鼠标事件-拖拽(滑块控制物体透明度变化)的主要内容,如果未能解决你的问题,请参考以下文章

js拖拽功能的实现

JS——拖拽盒子

JavaScript拖拽效果

js 实现拖拽元素

javafx的鼠标事件对于带有透明的图片在部分区域无效

18-8-1