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

<!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>

转载于:https://www.cnblogs.com/youcandomore/p/6785232.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值