当前位置: 首页> 科技> 能源 > 原生JS实现滑动到当前数字模块数字递增动画

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

时间:2025/7/12 6:07:52来源:https://blog.csdn.net/p_s_p/article/details/141430419 浏览次数:0次

源码:

两种递增分别是:

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实现滑动到当前数字模块数字递增动画

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: