JavaScript——PC端网页特效

本文详细介绍了JavaScript中关于元素偏移量offset、元素可视区client、元素滚动scroll的系列知识,包括各种属性和实战案例,如鼠标坐标、模态框拖动、京东放大镜效果。同时,深入探讨了动画函数的封装,包括实现原理、缓动效果,并提供了多个实用网页特效案例,如轮播图、返回顶部和筋斗云移动。
摘要由CSDN通过智能技术生成

目录

一、元素偏移量offset系列

1. offset概述

2. 常见属性

3. offset与style区别

案例——鼠标在盒子内坐标

案例——拖动模态框

案例——京东放大镜

二、元素可视区client系列

flexible源码分析

立即执行函数

pageshow事件

三、元素滚动scroll系列

1. 页面被卷去头部

 案例——仿淘宝固定右侧侧边栏

 2. 页面被卷曲头部兼容性方案

四、三大系列总结

五、mouseenter和mouseover事件

六、动画函数封装

1. 动画实现原理

2. 动画函数的简单封装

3. 动画函数给不同元素记录不同定时器

4. 缓动效果动画

5. 动画函数在多个目标之间移动

6. 给动画函数添加回调参数

7. 动画函数封装到单独JS文件里面

案例——右侧盒子滑动

七、常见网页特效案例

案例——网页轮播图

1. 节流阀

短路运算简写callback函数

案例——返回顶部

案例——筋斗云移动


  

一、元素偏移量offset系列

1. offset概述

offset翻译过来就是偏移量,我们使用offset系列相关属性可以动态的得到该元素的位置(偏移)、大小等。

  • 获取元素距离带有定位父元素的位置
  • 获得元素自身大小
  • 注意:返回的值都不带单位

2. 常见属性

	<!-- offset位置 -->
	<style type="text/css">
		* {
			margin: 0;
			padding: 0;
		}

		.father {
			position: relative;
			width: 200px;
			height: 200px;
			background-color: pink;
			margin: 150px;
		}

		.son {
			width: 50px;
			height: 50px;
			background-color: blue;
			margin: 50px;
			padding: 10px;
			border: 2px solid red;
		}
	</style>
	<body>
		<div class="father">
			<div class="son">

			</div>
		</div>
		<script type="text/javascript">
			var father = document.querySelector('.father');
			var son = document.querySelector('.son');

			console.log(father.offsetTop);
			console.log(father.offsetLeft);
			//1.它是以有定位的父亲为准,若无父亲或无定位的父亲 以body为准
			console.log(son.offsetLeft);
			//2.可以得到元素的大小,宽高  包含padding+border+width
			console.log(son.offsetHeight);
			console.log(son.offsetWidth);
			//3. 返回带有定位的父亲,若无返回body
			console.log(son.offsetParent);
			console.log(son.parentNode);	//返回父亲 是最近一级的父亲 不管有没有定位
		</script>
	</body>

3. offset与style区别

案例——鼠标在盒子内坐标

    <!-- 鼠标在盒子内坐标 -->
	<style type="text/css">
		.box {
			width: 200px;
			height: 200px;
			background-color: pink;
			margin: 100px 100px;
		}
	</style>
	<body>
		<div class="box"></div>
		<script type="text/javascript">
			var box = document.querySelector('.box');
			box.addEventListener('mousemove', function(e) {
				// console.log(e.pageX);
				// console.log(e.pageY);
				var x = e.pageX - this.offsetLeft;
				var y = e.pageY - this.offsetTop;
				this.innerHTML = 'x坐标是' + x + 'y坐标是' + y;
			})
		</script>
	</body>

案例——拖动模态框

	<!-- 拖动模态框 -->
	<style type="text/css">
		.login-header {
			text-align: center;
		}

		a {
			margin: 0;
			padding: 0;
		}

		.login {
			display: none;
			width: 512px;
			height: 280px;
			position: fixed;
			border: 1px solid #EBEBEB;
			left: 50%;
			top: 50%;
			background-color: #FFFFFF;
			box-shadow: 0 0 20px #DDDDDD;
			z-index: 9999;
			transform: translate(-50%, -50%);
		}

		.login-title {
			width: 100%;
			margin: 10px 0 0 0;
			text-align: center;
			line-height: 40px;
			height: 40px;
			font-size: 18px;
			position: relative;
			cursor: move;
		}

		.login-input-content {
			margin-top: 20px;
		}

		.login-button {
			width: 50%;
			margin: 30px auto 0 auto;
			line-height: 40px;
			font-size: 14px;
			border: 1px solid #EBEBEB;
			text-align: center;
		}

		.login-bg {
			display: none;
			width: 100%;
			height: 100%;
			position: fixed;
			top: 0px;
			left: 0px;
			background-color: rgba(0, 0, 0, .3);
		}

		a {
			text-decoration: none;
			color: #000000;
		}

		.login-button a {
			display: block;
		}

		.login-input input.list-input {
			float: left;
			line-height: 35px;
			height: 35px;
			width: 350px;
			border: 1px solid #EBEBEB;
			text-indent: 5px;
		}

		.login-input {
			overflow: hidden;
			margin: 0 0 20px 0;
		}

		.login-input label {
			float: left;
			width: 90px;
			padding-right: 10px;
			text-align: center;
			line-height: 35px;
			height: 35px;
			font-size: 14px;
		}

		.login-title span {
			position: absolute;
			font-size: 12px;
			right: -20px;
			top: -30px;
			background-color: #FFFFFF;
			border: #EBEBEB solid 1px;
			width: 40px;
			height: 40px;
			border-radius: 20px;
		}
	</style>
	<body>
		<div class="login-header"><a id="link" href="javascript:;">点击,弹出登录框</a></div>
		<div id="login" class="login">
			<div id="title" class="login-title">登录会员
				<span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span>
			</div>
			<div class="login-input-content">
				<div class="login-input">
					<label>用户名:</label>
					<input type="text" name="info[username]" id="username" placeholder="请输入用户名" class="list-input" />
				</div>
				<div class="login-input">
					<label>登录密码:</label>
					<input type="password" placeholder="请输入登录密码" name="info[password]" id="password"
						class="list-input" />
				</div>
			</div>
			<div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录</a></div>
		</div>

		<!-- 遮盖层 -->
		<div id="bg" class="login-bg"></div>

		<script>
			//1.获取事件
			var login = document.querySelector('.login');
			var mask = document.querySelector('.login-bg');
			var link = document.querySelector('#link');
			var closeBtn = document.querySelector('#closeBtn');
			var title = document.querySelector('#title');
			//2.点击弹出,显示登录
			link.addEventListener('click', function() {
				mask.style.display = 'block';
				login.style.display = 'block';
			})
			//3.点击关闭
			closeBtn.addEventListener('click', function() {
				mask.style.display = 'none';
				login.style.display = 'none';
			})
			//4.开始拖拽
			//(1)当鼠标按下,获得鼠标在盒子内坐标
			title.addEventListener('mousedown', function(e) {
				var x = e.pageX - login.offsetLeft;
				var y = e.pageY - login.offsetTop;
				//(2)鼠标移动式,把鼠标在页面中的坐标,减去鼠标在盒子内的坐标(不变的)
				function move(e) {
					login.style.left = e.pageX - x + 'px';
					login.style.top = e.pageY - y + 'px';
				}
				document.addEventListener('mousemove', move);
				//(3)鼠标弹起,就让鼠标移动事件移除
				document.addEventListener('mouseup', function() {
					document.removeEventListener('mousemove', move);
				})
			})
		</script>

案例——京东放大镜

	<style type="text/css">
		* {
			margin: 0;
			padding: 0;
		}

		.preview_img {
			position: relative;
			border: 1px solid #CCCCCC;
			height: 400px;
			width: 400px;
		}

		.preview img {}

		.mask {
			display: none;
			position: absolute;
			top: 0;
			left: 0;
			height: 300px;
			width: 300px;
			background-color: #fede4f;
			border: 1px solid #CCCCCC;
			opacity: .5;
			cursor: move;
		}

		.big {
			display: none;
			position: absolute;
			top: 0;
			left: 410px;
			width: 500px;
			height: 500px;
			border: 1px solid #CCCCCC;
			overflow: hidden;
		}

		.big img {
			position: absolute;
			top: 0;
			left: 0;
		}
	</style>
	<body>
		<div class="preview_img">
			<img src="img/load.png" />
			<div class="mask">1</div>
			<div class="big">
				<img src="img/load-big.jpg" class="bigImg">
			</div>
		</div>
	</body>
	<script type="text/javascript">
		var preview_img = document.querySelector('.preview_img');
		var mask = document.querySelector('.mask');
		var big = document.querySelector('.big');
		//1.当鼠标经过preview_img 就显示隐藏mask遮挡层,和big大盒子
		preview_img.addEventListener('mouseover', function() {
			mask.style.display = 'block';
			big.style.display = 'block';
		})
		preview_img.addEventListener('mouseout', function() {
			mask.style.display = 'none';
			big.style.display = 'none';
		})
		//2.鼠标移动让黄色盒子跟着鼠标走
		preview_img.addEventListener('mousemove', function(e) {
			//(1)先计算出鼠标在盒子内的坐标
			var x = e.pageX - this.offsetLeft;
			var y = e.pageY - this.offsetTop;
			//(2)减去盒子高度的一般,保证鼠标在中间
			var maskX = x - mask.offsetWidth / 2;
			var maskY = y - mask.offsetHeight / 2;
			//(3)黄色盒子不能出小盒子范围。
			//(3).1 如果x坐标小于0就让停在0的位置,大于小盒子宽度减去遮挡层宽度
			//遮挡层最大移动距离(正方形,长宽一样)
			var maskMax = preview_img.offsetWidth - mask.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值