获取鼠标坐标

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>获取鼠标坐标</title>
		<style>
			#mousedot{width:20px;height:20px;background:#ccc;position:absolute;top:0;left:0;}
		</style>
	</head>
	<body>
		<div id="mousedot"></div>
	</body>
</html>
<script>
	var mousedot=document.getElementById("mousedot");
	
	function mousePosition(e){
		if(e.pageX){
			//ie9及以上,其他浏览器均支持pageX属性
return {x:e.pageX,y:e.pageY};
		}else{
			return{
				x:e.clientX + document.body.scrollLeft - document.body.clientLeft,
				y:e.clientY + document.body.scrollTop - document.body.clientTop,
			}
		}
	}
	function mousemove(e){
		var e=e|| window.event;
		var mosusepos=mousePosition(e);
		mousedot.style.left=mosusepos.x-mousedot.clientWidth/2+"px";
		mousedot.style.top=mosusepos.y-mousedot.clientHeight/2+"px";
	}
	
	document.οnmοusemοve=mousemove;
		
</script>

1、坐标值:

  pageX:触发点相对于文档左上角的位置

  clientX:触发点相对于浏览器左上角的位置

  screenX:触发点相对于屏幕左上角的位置

2、原生js获取元素的宽高:

  clientWidth、clientHeight 元素可视区的大小

 

转载于:https://www.cnblogs.com/xiaozhuzhuandxiaomoney/p/7338174.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值