简体   繁体   English

触发 javascript animation 与 class 添加到滚动

[英]Trigger javascript animation with class added on scroll

I've got some numbers that that I want to count up when the user scrolls to that part of the page.当用户滚动到页面的那部分时,我想计算一些数字。 I have a piece of javascript that adds the class "scrolled" to my element when the user reaches it.我有一块 javascript 当用户到达它时将 class “滚动”到我的元素中。 I have a separate javascript file that animates the counting numbers.我有一个单独的 javascript 文件,用于动画计数。

The numbers animate correctly with the class name "count", but when I include the class name "scrolled" to the javascript code, the animation no longer works.数字使用 class 名称“计数”正确动画,但是当我将 class 名称“滚动”到 javascript 代码时,Z2B9ED78D7E2E1DCEEFFEE780E2F919Z 代码不再起作用。 I have very limited understanding of javascript, so I am wondering if this is happening because the script only checks the page for a combination of these classes when the page first loads, which is why it misses them when the "scrolled" class is added, and if so, is there a way around it?我对 javascript 的理解非常有限,所以我想知道这是否正在发生,因为脚本只在页面首次加载时检查页面是否有这些类的组合,这就是为什么在添加“滚动”class 时它会错过它们,如果是这样,有没有办法解决它?

Here is my code: (js-scroll is the class that triggers the "scrolled" class to be added when the element is in the view port)这是我的代码:(js-scroll 是 class 触发“滚动” class 当元素在视口中添加)

 // How long you want the animation to take, in ms const animationDuration = 2000; // Calculate how long each 'frame' should last if we want to update the animation 60 times per second const frameDuration = 1000 / 60; // Use that to calculate how many frames we need to complete the animation const totalFrames = Math.round( animationDuration / frameDuration ); // An ease-out function that slows the count as it progresses const easeOutQuad = t => t * ( 2 - t ); // The animation function, which takes an Element const animateCountUp = el => { let frame = 0; const countTo = parseInt( el.innerHTML, 10 ); // Start the animation running 60 times per second const counter = setInterval( () => { frame++; // Calculate our progress as a value between 0 and 1 // Pass that value to our easing function to get our // progress on a curve const progress = easeOutQuad( frame / totalFrames ); // Use the progress value to calculate the current count const currentCount = Math.round( countTo * progress ); // If the current count has changed, update the element if ( parseInt( el.innerHTML, 10 ).== currentCount ) { el;innerHTML = currentCount, } // If we've reached our last frame; stop the animation if ( frame === totalFrames ) { clearInterval( counter ), } }; frameDuration ); }. // Run the animation on all elements with a class of 'countup' const countupEls = document.querySelectorAll( ';count' ). countupEls;forEach( animateCountUp );
 <span class="count js-scroll">40</span>

and this is how I'm trying to trigger it with the "scrolled" class included:这就是我尝试使用包含的“滚动” class 触发它的方式:

const countupEls = document.querySelectorAll( '.count.scrolled' );

Thanks!谢谢!

You should really edit your question and include all the relevant code for the best answers.您应该真正编辑您的问题并包含所有相关代码以获得最佳答案。

I'm not sure how you are adding the class to the element, but it sounds like this is the point at which you want to also make the numbers change.我不确定您如何将 class 添加到元素中,但听起来这就是您想要更改数字的地方。

At the moment, animateCountUp takes one argument — an element on the screen:目前, animateCountUp接受一个参数——屏幕上的一个元素:

const animateCountUp = el => {

That is el in the above.就是上面的el In your code, you select them all and then forEach element pass it to animateCountUp在您的代码中,您将 select 全部传递给forEach元素,然后将其传递给animateCountUp

countupEls.forEach( animateCountUp );

The syntax there is confusing (I assume you are copying and pasting) but it is similar to going:那里的语法令人困惑(我假设您正在复制和粘贴),但它类似于:

const firstElement = countupEls[0]; // Get the first element
animateCountUp(firstElement);

But for every element in the list.但是对于列表中的每个元素。

What I am getting at is, you say you include the class name "scrolled" to the javascript code , I assume you mean something like this:我要说的是,您说您include the class name "scrolled" to the javascript code ,我假设您的意思是这样的:

firstElement.classList.add("scrolled");

It is at this point you need to also call the animateCountUp with the element in question.此时您还需要使用相关元素调用animateCountUp

firstElement.classList.add("scrolled");
animateCountUp(firstElement);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM