PC端网页特效(三)

PC端网页特效(三)

1.缓动效果原理

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

思路:

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

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

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

4. 步长值取整(正值向上取整,负值向下取整)

例子:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no,
		initial-sale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
		<title></title>
		<style>
			div {
				position: absolute;
				left: 0;
				width: 100px;
				height: 100px;
				background-color: pink;
			}
			span {
				position: absolute;
				left: 0;
				top: 200px;
				display: block;
				width: 150px;
				height: 150px;
				background-color: purple;
			}
		</style>
	</head>
	<body>
	    <button class="btn500">点击夏雨荷到500</button>
	    <button class="btn800">点击夏雨荷到800</button>
	    <span>夏雨荷</span>
	    <script>
	        // 缓动动画函数封装obj目标对象 target 目标位置
	        // 思路:
	        // 1. 让盒子每次移动的距离慢慢变小, 速度就会慢慢落下来。
	        // 2. 核心算法:(目标值 - 现在的位置) / 10 做为每次移动的距离 步长
	        // 3. 停止的条件是: 让当前盒子位置等于目标位置就停止定时器
	        function animate(obj, target) {
	            // 先清除以前的定时器,只保留当前的一个定时器执行
	            clearInterval(obj.timer);
	            obj.timer = setInterval(function() {
	                // 步长值写到定时器的里面
	                // 把我们步长值改为整数 不要出现小数的问题
	                // var step = Math.ceil((target - obj.offsetLeft) / 10);
	                var step = (target - obj.offsetLeft) / 10;
	                step = step > 0 ? Math.ceil(step) : Math.floor(step);
	                if (obj.offsetLeft == target) {
	                    // 停止动画 本质是停止定时器
	                    clearInterval(obj.timer);
	                }
	                // 把每次加1 这个步长值改为一个慢慢变小的值  步长公式:(目标值 - 现在的位置) / 10
	                obj.style.left = obj.offsetLeft + step + 'px';
	
	            }, 15);
	        }
	        var span = document.querySelector('span');
	        var btn500 = document.querySelector('.btn500');
	        var btn800 = document.querySelector('.btn800');
	
	        btn500.addEventListener('click', function() {
	            // 调用函数
	            animate(span, 500);
	        })
	        btn800.addEventListener('click', function() {
	                // 调用函数
	                animate(span, 800);
	            })
	            // 匀速动画 就是 盒子是当前的位置 +  固定的值 10 
	            // 缓动动画就是  盒子当前的位置 + 变化的值(目标值 - 现在的位置) / 10)
	    </script>
	</body>
</html>

在这里插入图片描述

2.动画函数添加回调函数

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

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

上面的例子加回调函数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no,
		initial-sale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
		<title></title>
		<style>
			div {
				position: absolute;
				left: 0;
				width: 100px;
				height: 100px;
				background-color: pink;
			}
			span {
				position: absolute;
				left: 0;
				top: 200px;
				display: block;
				width: 150px;
				height: 150px;
				background-color: purple;
			}
		</style>
	</head>
	<body>
	    <button class="btn500">点击夏雨荷到500</button>
	    <button class="btn800">点击夏雨荷到800</button>
	    <span>夏雨荷</span>
	    <script>
	        // 缓动动画函数封装obj目标对象 target 目标位置
	        // 思路:
	        // 1. 让盒子每次移动的距离慢慢变小, 速度就会慢慢落下来。
	        // 2. 核心算法:(目标值 - 现在的位置) / 10 做为每次移动的距离 步长
	        // 3. 停止的条件是: 让当前盒子位置等于目标位置就停止定时器
	        function animate(obj, target, callback) {
	            // console.log(callback);  callback = function() {}  调用的时候 callback()
	
	            // 先清除以前的定时器,只保留当前的一个定时器执行
	            clearInterval(obj.timer);
	            obj.timer = setInterval(function() {
	                // 步长值写到定时器的里面
	                // 把我们步长值改为整数 不要出现小数的问题
	                // var step = Math.ceil((target - obj.offsetLeft) / 10);
	                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();
	                    }
	                }
	                // 把每次加1 这个步长值改为一个慢慢变小的值  步长公式:(目标值 - 现在的位置) / 10
	                obj.style.left = obj.offsetLeft + step + 'px';
	
	            }, 15);
	        }
	        var span = document.querySelector('span');
	        var btn500 = document.querySelector('.btn500');
	        var btn800 = document.querySelector('.btn800');
	
	        btn500.addEventListener('click', function() {
	            // 调用函数
	            animate(span, 500, function(){
					span.style.backgroundColor = 'green';
				});
	        })
	        btn800.addEventListener('click', function() {
	                // 调用函数
	                animate(span, 800, function() {
	                    span.style.backgroundColor = 'red';
	                });
	            })
	            // 匀速动画 就是 盒子是当前的位置 +  固定的值 10 
	            // 缓动动画就是  盒子当前的位置 + 变化的值(目标值 - 现在的位置) / 10)
	    </script>
	</body>
</html>

在这里插入图片描述

3.调用动画函数

首先,封装动画函数文件animate.js

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();
            }
        }
        // 把每次加 这个步长值改为一个慢慢变小的值  步长公式:(目标值 - 现在的位置) / 10
        obj.style.left = obj.offsetLeft + step + 'px';

    }, 15);
}

调用animate动画函数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no,
		initial-sale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
		<title></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="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 就会让 con这个盒子滑动到右侧
	        sliderbar.addEventListener('mouseenter', function() {
	            // animate(obj, target, callback);
	            animate(con, -160, function() {
	                // 当我们动画执行完毕,就把 ← 改为 →
	                sliderbar.children[0].innerHTML = '→';
	            });
	
	        })
	        sliderbar.addEventListener('mouseleave', function() {
	            // animate(obj, target, callback);
	            animate(con, 0, function() {
					// 当我们动画执行完毕,就把 → 改为 ←
	                sliderbar.children[0].innerHTML = '←';
	            });
	
	        })
	    </script>
	</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值