前端路线--JavaScript篇(Day09)

mouseEvent属性

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
		*{
			margin: 0;
			padding: 0;
		}
			#box{
				width: 100px;
				height: 100px;
				background-color: aqua;
			}
			.box2{
				height: 2000px;
			}
		</style>
	</head>
	<body>
		<div id="box"></div>
		<div class="box2"></div>
		
		<script type="text/javascript">
		var box1=document.getElementById('box');
		box1.onclick=function (e) {
			//鼠标距离可视区(body)的位置
			console.log(e.clientX);
			console.log(e.clientY);
			
			//整个页面(包含被卷上去)的目标
			console.log('-------');
			console.log(e.pageX);
			console.log(e.pageY);
			
			//距离屏幕的位置
			console.log('------');
			console.log(e.screenX);
			console.log(e.screenY);
			
			//距离事件源的坐标
			console.log('------');
			console.log(e.offsetX);
			console.log(e.offsetY);
		}
			
		</script>
	</body>
</html>

案例–鼠标跟随

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
		*{
			margin: 0;
			padding: 0;
		}
			#follow{
				width: 100px;
				height: 100px;
				background-color: pink;
				position: absolute;
				left: 0px;
				top: 0px;
				cursor: move;
			}
		</style>
	</head>
	<body>
		<div id="follow"></div>
		
		<script type="text/javascript">
			var box=document.getElementById('follow');
			//盒子要加定位
			//利用pageX和pageY给盒子设置top和left
			window.onmousemove=function (e) {
				box.style.left=e.pageX-box.offsetWidth/2+'px';
				box.style.top=e.pageY-box.offsetHeight/2+'px';
			}
		</script>
	</body>
</html>

元素的offset属性

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
		*{
			margin: 0px;
			padding: 0px;
		}
			.father{
				width: 300px;
				height: 300px;
				background-color: blue;
				position: relative;
			}
			#son{
				width: 100px;
				height: 100px;
				background-color: red;
				position: absolute;
				left: 30px;
				top: 30px;
			}
		</style>
	</head>
	<body>
		<div class="father">
			<div id="son">
				
			</div>
		</div>
	</body>
	<script type="text/javascript">
		var son=document.getElementById('son');
		son.onclick=function() {
			//这是元素自身的属性,不需要e
			
			//元素相对于父元素(定位)的左偏移量         !包含自己的外边距!
			console.log(son.offsetLeft);
			console.log(son.offsetTop);
			
			//元素自身的宽高                           !包含pdding和border!
			console.log('-------');
			console.log(son.offsetWidth);
			console.log(son.offsetHeight);
			
			//父盒子(相对定位,或者body)
			console.log('------');
			console.log(son.offsetParent);
		}
	</script>
</html>

点击拖拽案例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#box{
				width: 100px;
				height: 100px;
				background-color: #00FFFF;
				position: absolute;
				left: 0px;
				top: 0px;
			}
		</style>
	</head>
	<body>
		<div id="box"></div>
	</body>
	<script type="text/javascript">
		var box=document.getElementById('box');
		//鼠标按下事件获取鼠标在盒子里的值(page-盒子与浏览器的距离:    e.pageX-box.offsetLeft)
		box.onmousedown=function (e) {
			var mouseLeft=e.pageX-box.offsetLeft;
			var mouseTop=e.pageY-box.offsetTop;
			//鼠标拖拽(拖拽的地方最后盒子的位置是:盒子最后的位置=新的page-坐标在盒子里的距离)
			window.onmousemove=function (e) {
				box.style.left=e.pageX-mouseLeft+'px';
				box.style.top=e.pageY-mouseTop+'px';
			}
			box.onmouseup=function () {
				window.onmousemove=null;
			}
		}
	</script>
</html>

案例–放大镜

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			*{
				margin: 0px;
				padding: 0px;
			}
			#minBox{
				width: 450px;
				height: 450px;
				border: 1px solid orange;
				position: relative;
				top: 50px;
				left: 50px;
			}
			#maskBox{
				width: 200px;
				height: 200px;
				background-color: orange;
				opacity: 0.5;
				position: absolute;
				top: 0px;
				left: 0px;
				display: none;
			}
			#maxBox{
				width: 600px;
				height: 600px;
				border: 1px solid blue;
				position: absolute;
				left: 560px;
				top: 0px;
				overflow: hidden;
				display: none;
				
			}
			#maxIamge{
				position: relative;
			}
			
		</style>
	</head>
	<body>
		<!-- 小盒子 -->
		<div id="minBox">
			<img src="img/min.jpg" >
			<div id="maskBox"></div>
		</div>
		<!-- 大盒子 -->
		<div id="maxBox">
			<img src="img/max.jpg"  id="maxIamge">
		</div>
	</body>
	
	
	<script type="text/javascript">
	var minBox=document.getElementById('minBox');
	var maskBox=document.getElementById('maskBox');
	var maxBox=document.getElementById('maxBox');
		// 1 鼠标悬停,蒙版显示, 大盒子显示
		minBox.onmouseover=function (e) {
			maskBox.style.display='block';
			maxBox.style.display='block';
		}
		
		// 2 蒙版在小盒子中移动
		minBox.onmousemove=function (e) {
			//需要再减去图片外盒子的偏移量来保持鼠标居中
			//e.pageX-minBox.offsetLeft-maskBox.offsetWidth/2--->盒子在左上角的坐标(mask盒子的最小移动距离)
			var minLeft=e.pageX-minBox.offsetLeft-maskBox.offsetWidth/2;
			var minTop=e.pageY-minBox.offsetTop-maskBox.offsetHeight/2;
			//盒子能移动的最大距离
			var maxMoveLeft=minBox.offsetWidth-maskBox.offsetWidth;
			var maxMoveTop=minBox.offsetHeight-maskBox.offsetHeight;
			
			//设置判断判断不让mask盒子超出minBox
			//水平方向
			if(minLeft<=0){
				minLeft=0;
			}else if(minLeft>maxMoveLeft){
				minLeft=maxMoveLeft;
			}
			//竖直方向
			if(minTop<=0){
				minTop=0;
			}else if(minTop>maxMoveTop){
				minTop=maxMoveTop;
			}
			maskBox.style.left=minLeft+'px';
			maskBox.style.top=minTop+'px';	
			
			
			// 3 图片在大盒子中移动
			var maxImg=document.getElementById('maxIamge');
			var rateX=minLeft/maxMoveLeft;
			var rateY=minTop/maxMoveTop;
			//图片的移动范围(变)/minBox宽-mask的宽(可以定最大的距离)=x(大图的偏移量)/大图宽-大盒子的宽
			maxImg.style.left=-(maxImg.offsetWidth-maxBox.offsetWidth)*rateX+'px';
			maxImg.style.top=-(maxImg.offsetHeight-maxBox.offsetHeight)*rateY+'px';
			
		}



		// 4 鼠标离开, 蒙版隐藏, 大盒子隐藏
		minBox.onmouseout=function (e) {
			maskBox.style.display='none';
			maxBox.style.display='none';
		}
	</script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值