首页 > 其他 > 详细

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

时间:2017-04-29 15:57:59      阅读:224      评论:0      收藏:0      [点我收藏+]

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

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

原文:http://www.cnblogs.com/youcandomore/p/6785232.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!