鼠标方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style>
.box {
width: 100px;
height: 100px;
background-color: orange;
}
</style>
</head>
<body>
<div class="box"> </div>
<br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br>
<div style="height: 1000px;"></div>
<script>
//鼠标行为->坐标系
//clientX/Y 鼠标位置相对于当前可视区域的坐标
// (不包含滚动条的距离)与滚动条状态无关
//layerX/Y 同pageXY相同 IE11以下同clientX/Y
//screenX/Y 鼠标位置相对于屏幕的坐标 整个屏幕
//与浏览器的位置 大小 全屏 半屏等有关
//x/y 同clientX/Y相同 FF不支持(火狐不支持)因为火狐浏览器不兼容所以最好别用
//pageX/pageY ie9以下不支持
//鼠标位置相当于当前文档的坐标(包含滚动条的距离)滚动条偏移的距离+clientX/Y
document.onclick = function (e) {//e是事件对象
var e = e || window.event;
// console.log(e)
// console.log(e.clientY, e.pageY);
console.log(e.screenY,e.pageY);
}
</script>
</body>
</html>
offsetX/Y
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style>
.box {
width: 100px;
height: 100px;
background-color: orange;
}
.box1{
position: absolute;
top: 200px;
left: 200px;
width: 100px;
height: 100px;
background-color: orange;
}
</style>
</head>
<body>
<div class="box"> </div>
<div class="box1"></div>
<br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br>
<div style="height: 1000px;"></div>
<script>
//鼠标行为->坐标系
//clientX/Y 鼠标位置相对于当前可视区域的坐标
// (不包含滚动条的距离)与滚动条状态无关
//layerX/Y 同pageXY相同 IE11以下同clientX/Y
//screenX/Y 鼠标位置相对于屏幕的坐标 整个屏幕
//与浏览器的位置 大小 全屏 半屏等有关
//x/y 同clientX/Y相同 FF不支持(火狐不支持)因为火狐浏览器不兼容所以最好别用
//pageX/pageY ie9以下不支持
//鼠标位置相当于当前文档的坐标(包含滚动条的距离)滚动条偏移的距离+clientX/Y
//offsetX/Y 鼠标位置相对于块元素的坐标 与相对的块元素有关 与其他的无关
//(包含边框,safari不包含边框)
var box=document.getElementsByClassName('box')[0];
var box1=document.getElementsByClassName('box1')[0];
box.onclick = function (e) {//e是事件对象
var e = e || window.event;
// console.log(e)
// console.log(e.clientY, e.pageY);
console.log(e.offsetX,e.offsetY);
}
box1.onclick = function(e) {
console.log(e.offsetX,e.offsetY);
}
</script>
</body>
</html>
兼容性
pagePos 整个页面,算上滚动条的获取鼠标方位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style>
.box {
width: 100px;
height: 100px;
background-color: orange;
}
.box1{
position: absolute;
top: 200px;
left: 200px;
width: 100px;
height: 100px;
background-color: orange;
}
</style>
</head>
<body>
<div class="box"> </div>
<div class="box1"></div>
<br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br>
<div style="height: 1000px;"></div>
<script type="text/javascript" src="./utils.js"></script>
<script>
var box=document.getElementsByClassName('box')[0];
document.onclick=function(e) {
var e=e||window.event;
console.log(e.pageY,pagePos(e).Y)
}
//整个页面,算上滚动条的获取鼠标方位
function pagePos(e){
var sLeft=getScrollOffset().left,
sTop=getScrollOffset().top,
cLeft=document.documentElement.clientLeft||0,
cTop=document.documentElement.clientTop || 0;
return {
X:e.clientX+sLeft-cLeft,
Y:e.clientY+sTop-cTop
}
}
</script>
</body>
</html>
onmousemove
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<style>
.box {
position: absolute;
top: 200px;
left: 200px;
width: 100px;
height: 100px;
background-color: orange;
}
</style>
<body>
<div class="box"></div>
<script type="text/javascript" src="./utils.js"></script>
<script>
var box = document.getElementsByClassName('box')[0];
console.log(box)
//mousedown mouseup mousemove
// box.onmousedown=function(e) {
// box.onmousemove=function(e) {
// }
// box.onmouseup=function(e) {
// }
// }
document.onmousemove = function(e) {
var e=e || window.event,
page=pagePos(e);
box.style.left=page.X+'px';
box.style.top = page.Y + "px";
}
</script>
</body>
</html>
偏移问题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<style>
.box {
position: absolute;
top: 200px;
left: 200px;
width: 100px;
height: 100px;
background-color: orange;
}
</style>
<body>
<div style="height: 1000px;"></div>
<div class="box"></div>
<script type="text/javascript" src="./utils.js"></script>
<script>
var box = document.getElementsByClassName('box')[0];
console.log(box)
//mousedown mouseup mousemove
/*缺陷:点击中间等box区域,box也会直接移动,因为把box左上角赋予鼠标点击的方位,
有时只是想点击方块,可以移动box,之后再移动,而不是直接移动
*/
// box.onmousedown=function(e) {
// document.onmousemove=function(e) {
// var e=e||window.event,
// page=pagePos(e);
// box.style.left=page.X + "px";
// box.style.top = page.Y + "px";
// }
// box.onmouseup=function(e) {
// document.onmousemove=null;
// }
// }
box.onmousedown=function(e) {
var e=e||window.event;
x=pagePos(e).X-getStyles(box,'left');
/*算上滚动条偏移的鼠标点击的坐标-算上滚动条box左上角的距离
=鼠标点击的坐标至box左上角的距离*/
y=pagePos(e).Y - getStyles(box, "top");
console.log(x,y)
document.onmousemove=function(e) {//冒泡事件
var e=e||window.event,
page=pagePos(e);
box.style.left=page.X -x+ "px";
box.style.top = page.Y-y + "px";
}
document.onmouseup=function(e) {
// document.onmousemove=null;
// document.onmouseup = null;
this.onmousemove=null;
this.onmouseup = null;
}
}
// document.onmousemove = function(e) {
// var e=e || window.event,
// page=pagePos(e);
// box.style.left=page.X+'px';
// box.style.top = page.Y + "px";
// }
</script>
</body>
</html>
封装拖拽函数
但拖拽边界有问题,下一课程会有解决之道 得重写 满足不了需求 重新封装
//拖拽 封装好 以后直接调用就行了
//有边界
function elemDrag(elem) {
var x,
y;
addEvent(elem, 'mousedown', function (e) {
// console.log('1');
var e = e || window.event;
// console.log(elem);
x = pagePos(e).X - getStyles(elem, 'left');
y = pagePos(e).Y - getStyles(elem, 'top');
addEvent(document, 'mousemove', mouseMove);
addEvent(document, 'mouseup', mouseUp);
cancelBubble(e);//保险起见 把阻止事件放进去
preventDefaultEvent(e);
});
function mouseMove(e) {
console.log(elem);
var e = e || window.event;
elem.style.top = pagePos(e).Y - y + 'px';
elem.style.left = pagePos(e).X - x + 'px';
}
function mouseUp(e) {
var e = e || window.event;
removeEvent(document, 'mousemove', mouseMove);
removeEvent(document, 'mouseup', mouseUp);
}
}