Web APIs:PC 端网页特效--动画函数封装

发布于:2022-10-23 ⋅ 阅读:(353) ⋅ 点赞:(0)

动画原理

核心原理:通过定时器 setInterval() 不断移动盒子位置

实现步骤:

1. 获得盒子当前位置

2. 让盒子在当前位置加上1个移动距离

3. 利用定时器不断重复这个操作

4. 加一个结束定时器的条件

5. 注意此元素需要添加定位,才能使用element.style.left

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			div{
				position: absolute;
				width:100px ;
				height: 100px;
				left: 0px;
				background-color: pink;
				border: 1px solid red;
			}
		</style>
	</head>
	<body>
		<div></div>
		<script>
			
			var div=document.querySelector('div');
			var timer=setInterval(function(){
				if(div.offsetLeft>=400){
					// 停止动画 本质是停止定时器
					clearInterval(timer);
				}
				div.style.left=div.offsetLeft+1+'px';
			},30)
			
		</script>
	</body>
</html>

  简单动画函数封装 

注意函数需要传递2个参数,动画对象移动到的距离

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			div{
				position: relative;
				width:100px ;
				height: 100px;
				left: 0px;
				background-color: pink;
				border: 1px solid red;
			}
			span{
				position: relative;
				display: block;
				left: 0;
				width: 150px;
				height: 150px;
				background-color: yellow;
			}
		</style>
	</head>
	<body>
		<div></div>
		<span>123</span>
		<script>
			// 简单动画封装obj目标函数 target目标位置
			var div=document.querySelector('div');
			var span=document.querySelector('span')
			function animate(obj,target){
				var timer=setInterval(function(){
					if(obj.offsetLeft>=target){
						// 停止动画 本质是停止定时器
						clearInterval(timer);
					}
					obj.style.left=obj.offsetLeft+1+'px';
				},30)
			}
			// 调用函数
			animate(div,300)
			animate(span,200)
			
		</script>
	</body>
</html>

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

如果多个元素都使用这个动画函数,每次都要var 声明定时器。我们可以给不同的元素使用不同的定时器(自 己专门用自己的定时器)。

核心原理:利用 JS 是一门动态语言,可以很方便的给当前对象添加属性。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			div{
				position: absolute;
				width:100px;
				height:100px;
				left: 0;
				background-color: pink;
				/* border: 1px solid red; */
			}
			span{
				position: absolute;
				display: block;
				left: 0;
				top: 200px;
				width: 150px;
				height: 150px;
				background-color: yellow;
			}
		</style>
	</head>
	<body>
		<button>点我123才走</button>
		<div></div>
		<span>123</span>
		<script>
			// 简单动画封装obj目标函数 target目标位置
			// 给不同的元素指定了不同的定时器
			var div=document.querySelector('div');
			var span=document.querySelector('span');
			var btn=document.querySelector('button');
			function animate(obj,target){
				// 当我们不断的点击按钮,这个元素的速度会越来越快,因为开启了太多的定时器
				// 解决方案就是让我们只有一个定时器执行 
				// 先清除以前的定时器,只有一个定时器
				clearInterval(obj.timer)
				obj.timer=setInterval(function(){
					if(obj.offsetLeft>=target){
						// 停止动画 本质是停止定时器
						clearInterval(obj.timer);
					}else{
						obj.style.left=obj.offsetLeft+1+'px';
					}		
				},30)
			}
			// 调用函数
			animate(div,300)
			// animate(span,200)
			btn.addEventListener('click',function(){
				animate(span,2000);
			})
			
			
		</script>
	</body>
</html>

 缓动动画原理

缓动动画就是让元素运动速度有所变化,最常见的是让速度慢慢停下来

思路:

1. 让盒子每次移动的距离慢慢变小,速度就会慢慢落下来。

2. 核心算法: (目标值 - 现在的位置 ) / 10 做为每次移动的距离 步长

就是每秒走的路 

3. 停止的条件是: 让当前盒子位置等于目标位置就停止定时器

4. 注意步长值需要取整

 缓动动画基本代码实现

匀速动画 就是 盒子是当前的位置+固定的值(例如10    )
缓动动画 就是 盒子是当前的位置+变化的值(目标-现在的位置)/10

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			div{
				position: absolute;
				width:100px;
				height:100px;
				left: 0;
				background-color: pink;
				/* border: 1px solid red; */
			}
			span{
				position: absolute;
				display: block;
				left: 0;
				top: 200px;
				width: 150px;
				height: 150px;
				background-color: yellow;
			}
		</style>
	</head>
	<body>
		<button>点我123才走</button>
		<div></div>
		<span>123</span>
		<script>
			// 简单动画封装obj目标函数 target目标位置
			// 给不同的元素指定了不同的定时器
			var div=document.querySelector('div');
			var span=document.querySelector('span');
			var btn=document.querySelector('button');
			function animate(obj,target){
				// 当我们不断的点击按钮,这个元素的速度会越来越快,因为开启了太多的定时器
				// 解决方案就是让我们只有一个定时器执行 
				// 先清除以前的定时器,只有一个定时器
				clearInterval(obj.timer)
				obj.timer=setInterval(function(){
					// 步长值写到定时器的里面
					var step=(target-obj.offsetLeft)/10;
					if(obj.offsetLeft==target){
						// 停止动画 本质是停止定时器
						clearInterval(obj.timer);
					}else{
						// 把1改为步长(目标值 - 现在的位置 ) / 10 做为每次移动的距离 步长
						obj.style.left=obj.offsetLeft+step+'px';
					}		
				},15)
			}
			// 调用函数
			animate(div,500)
			// animate(span,200)
			btn.addEventListener('click',function(){
				animate(span,2000);
			})
			//匀速动画 就是 盒子是当前的位置+固定的值(例如10	)
			//缓动动画 就是 盒子是当前的位置+变化的值(目标-现在的位置)/10
			
		</script>
	</body>
</html>

 缓动动画多个目标值之间移动

除法运算,涉及小数会出现问题 

向上取整 ,但这样有问题,如何倒的取,那么他就不对了

用三元运算符就可以了 

缓动动画添加回调函数

回调函数原理:函数可以作为一个参数。将这个函数作为参数传到另一个函数里面,当那个函数执行完之后, 再执行传进去的这个函数,这个过程就叫做回调。

回调函数写的位置:定时器结束的位置。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			div{
				position: absolute;
				width:100px;
				height:100px;
				left: 0;
				background-color: pink;
				/* border: 1px solid red; */
			}
			span{
				position: absolute;
				display: block;
				left: 0;
				top: 200px;
				width: 150px;
				height: 150px;
				background-color: yellow;
			}
		</style>
	</head>
	<body>
		<button class="btn500">点我500才走</button>
		<button class="btn800">点我800才走</button>
		<div></div>
		<span>123</span>
		<script>
			// 简单动画封装obj目标函数 target目标位置
			// 给不同的元素指定了不同的定时器
			var div=document.querySelector('div');
			var span=document.querySelector('span');
			var btn500=document.querySelector('.btn500');
			var btn800=document.querySelector('.btn800');
			function animate(obj,target,callBack){
				// console.log(callBack);callBack=function(){}  调用的时候 callback()
				// 当我们不断的点击按钮,这个元素的速度会越来越快,因为开启了太多的定时器
				// 解决方案就是让我们只有一个定时器执行 
				// 先清除以前的定时器,只有一个定时器
				clearInterval(obj.timer)
				obj.timer=setInterval(function(){
					// 步长值写到定时器的里面
					// 把步长改为整数 不要出现小数的问题
					var step=(target-obj.offsetLeft)/10;
					step=step>0 ? Math.ceil(step) :Math.floor(step);
					if(obj.offsetLeft==target){
						// 停止动画 本质是停止定时器
						clearInterval(obj.timer);
						// 回调函数写到定时器结束后面
						if(callBack){
							// 调用函数
							callBack();
						}
					}else{
						// 把1改为步长(目标值 - 现在的位置 ) / 10 做为每次移动的距离 步长
						obj.style.left=obj.offsetLeft+step+'px';
					}		
				},15)
			}
			// 调用函数
			// animate(div,500)
			// animate(span,200)
			btn500.addEventListener('click',function(){
				animate(span,500,function(){
					// alert(1)
					span.style.backgroundColor='red';
				});
			})
			btn800.addEventListener('click',function(){
				animate(span,800);
			})
			//匀速动画 就是 盒子是当前的位置+固定的值(例如10	)
			//缓动动画 就是 盒子是当前的位置+变化的值(目标-现在的位置)/10
			
		</script>
	</body>
</html>

动画函数的使用

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

因为以后经常使用这个动画函数,可以单独封装到一个JS文件里面,使用的时候引用这个JS文件即可。

1. 单独新建一个JS文件。 2. HTML文件引入 JS 文件。

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .sliderbar {
            position: fixed;
            right: 0;
            bottom: 100px;
            width: 40px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            cursor: pointer;
            color: #fff;
        }
        
        .con {
            position: absolute;
            left: 0;
            top: 0;
            width: 200px;
            height: 40px;
            background-color: purple;
            z-index: -1;
        }
    </style>
    <script src="js/animate.js"></script>
</head>

<body>
    <div class="sliderbar">
        <span>←</span>
        <div class="con">问题反馈</div>
    </div>

    <script>
        // 1. 获取元素
        var sliderbar = document.querySelector('.sliderbar');
        var con = document.querySelector('.con');
        // 当我们鼠标经过 sliderbar 就会让 con这个盒子滑动到左侧
		sliderbar.addEventListener('mouseenter',function(){
			animate(con,-160,function(){
				sliderbar.children[0].innerHTML='>'
			})
		})
        // 当我们鼠标离开 sliderbar 就会让 con这个盒子滑动到右侧
		sliderbar.addEventListener('mouseleave',function(){
			animate(con,0,function(){
				sliderbar.children[0].innerHTML='<'
			})
		})
    </script>
</body>

</html>

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到