原生JS实现滑动到当前数字模块数字递增动画

2024-08-23 09:12

本文主要是介绍原生JS实现滑动到当前数字模块数字递增动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

源码:

两种递增分别是:

1、百分数递增

2、数字递增后添加文案


<div style="height: 1500px;"></div>  
<p class="number-module2" data-start="0" data-end="90" data-duration="1000">0%</p>  
<p class="number-module3" data-start="0" data-end="2" data-duration="1000">0</p>
<p class="number-module1" data-start="0" data-end="200" data-duration="1000">0</p><div style="height: 500px;"></div>  <script>
// w+
document.addEventListener("DOMContentLoaded", function() {  const numberModules1 = document.querySelectorAll('.number-module1');  const observer = new IntersectionObserver((entries) => {  entries.forEach(entry => {  if (entry.isIntersecting) {  const module = entry.target;  const start = parseInt(module.getAttribute('data-start'), 10);  const end = parseInt(module.getAttribute('data-end'), 10);  const duration = parseInt(module.getAttribute('data-duration'), 10);  animateNumber1(module, start, end, duration, 'w+');    // 如果你只想触发一次动画,可以取消观察  observer.unobserve(module);  }  });  });  numberModules1.forEach(module => observer.observe(module));  
});  function animateNumber1(element, start, end, duration, suffix = '') {  let startTime = null;  const animation1 = function(currentTime) {  if (startTime === null) startTime = currentTime;  const progress = Math.min((currentTime - startTime) / duration, 1);  const value = Math.floor(progress * (end - start) + start);  element.textContent = value.toString();  if (progress < 1) {  requestAnimationFrame(animation1);  } else {// 动画结束时添加 w+element.textContent += suffix;}};  requestAnimationFrame(animation1);  
}// /
document.addEventListener("DOMContentLoaded", function() {  const numberModules3 = document.querySelectorAll('.number-module3');  const observer = new IntersectionObserver((entries) => {  entries.forEach(entry => {  if (entry.isIntersecting) {  const module = entry.target;  const start = parseInt(module.getAttribute('data-start'), 10);  const end = parseInt(module.getAttribute('data-end'), 10);  const duration = parseInt(module.getAttribute('data-duration'), 10);  animateNumber3(module, start, end, duration, '/3');   // 如果你只想触发一次动画,可以取消观察  observer.unobserve(module);  }  });  });  numberModules3.forEach(module => observer.observe(module));  
});  function animateNumber3(element, start, end, duration, suffix = '') {  let startTime = null;  const animation3 = function(currentTime) {  if (startTime === null) startTime = currentTime;  const progress = Math.min((currentTime - startTime) / duration, 1);  const value = Math.floor(progress * (end - start) + start);  element.textContent = value.toString();  if (progress < 1) {  requestAnimationFrame(animation3);  } else {// 动画结束时添加 /3element.textContent += suffix;} };  requestAnimationFrame(animation3);  
}// %
document.addEventListener("DOMContentLoaded", function() {
const numberModules2 = document.querySelectorAll('.number-module2');
const observer = new IntersectionObserver((entries) => {  entries.forEach(entry => {  if (entry.isIntersecting) {  const module = entry.target;  const start = parseInt(module.getAttribute('data-start'), 10);  const end = parseInt(module.getAttribute('data-end'), 10);  const duration = parseInt(module.getAttribute('data-duration'), 10);  animateNumber2(module, start, end, duration);  // 如果你只想触发一次动画,可以取消观察  observer.unobserve(module);  }  });  
});  numberModules2.forEach(module => observer.observe(module));
});function animateNumber2(element, start, end, duration) {
let startTime = null;
const animation2 = function(currentTime) {  if (startTime === null) startTime = currentTime;  const progress = Math.min((currentTime - startTime) / duration, 1);  const value = Math.floor(progress * (end - start) + start);  // 将值转换为百分数并更新元素文本  element.textContent = value + '%';  if (progress < 1) {  requestAnimationFrame(animation2);  }  
};  requestAnimationFrame(animation2);
}
</script>

这篇关于原生JS实现滑动到当前数字模块数字递增动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1098960

相关文章

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

MySQL多列IN查询的实现

《MySQL多列IN查询的实现》多列IN查询是一种强大的筛选工具,它允许通过多字段组合快速过滤数据,本文主要介绍了MySQL多列IN查询的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析与优化1.

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

Python实现自动化接收与处理手机验证码

《Python实现自动化接收与处理手机验证码》在移动互联网时代,短信验证码已成为身份验证、账号注册等环节的重要安全手段,本文将介绍如何利用Python实现验证码的自动接收,识别与转发,需要的可以参考下... 目录引言一、准备工作1.1 硬件与软件需求1.2 环境配置二、核心功能实现2.1 短信监听与获取2.

使用Python实现获取网页指定内容

《使用Python实现获取网页指定内容》在当今互联网时代,网页数据抓取是一项非常重要的技能,本文将带你从零开始学习如何使用Python获取网页中的指定内容,希望对大家有所帮助... 目录引言1. 网页抓取的基本概念2. python中的网页抓取库3. 安装必要的库4. 发送HTTP请求并获取网页内容5. 解

mysql如何查看当前连接数

《mysql如何查看当前连接数》:本文主要介绍mysql如何查看当前连接数问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql查看当前连接数查看mysql数据库允许最大连接数总结mysql查看当前连接数查看当前连接数SHOW STATUS LIKE

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

Python实现Microsoft Office自动化的几种方式及对比详解

《Python实现MicrosoftOffice自动化的几种方式及对比详解》办公自动化是指利用现代化设备和技术,代替办公人员的部分手动或重复性业务活动,优质而高效地处理办公事务,实现对信息的高效利用... 目录一、基于COM接口的自动化(pywin32)二、独立文件操作库1. Word处理(python-d

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

使用Python实现网络设备配置备份与恢复

《使用Python实现网络设备配置备份与恢复》网络设备配置备份与恢复在网络安全管理中起着至关重要的作用,本文为大家介绍了如何通过Python实现网络设备配置备份与恢复,需要的可以参考下... 目录一、网络设备配置备份与恢复的概念与重要性二、网络设备配置备份与恢复的分类三、python网络设备配置备份与恢复实