简体   繁体   English

jQuery function 在我滚动页面时停止工作

[英]jQuery function stops working when I scroll the page

I have some progress bars and I need them to start loading when they are scrolled on.我有一些进度条,我需要它们在滚动时开始加载。 However, the jQuery function only works when the page loads on that section only, and when I scroll the page even slightly (while it is still in the view port) the function stops working.但是,jQuery function 仅在页面仅加载到该部分时才有效,并且当我稍微滚动页面时(当它仍在视口中时)ZC1C425268E68385D1AB5074C17A94F4 停止工作。 If the page loads from the top and I scroll down to reach this section, it does not work at all.如果页面从顶部加载并且我向下滚动到此部分,则它根本不起作用。

I have tried different approaches to make it work when it is in view port, all of which resulted in the same issue.我尝试了不同的方法让它在视口中工作,所有这些都导致了同样的问题。 I have also tried $(window).on("resize scroll", function ()).我也试过 $(window).on("resize scroll", function ())。

Any ideas on how it can be fixed?关于如何修复它的任何想法?

 $.fn.isInViewport = function() { var elementTop = $(this).offset().top; var elementBottom = elementTop + $(this).outerHeight(); var viewportTop = $(window).scrollTop(); var viewportBottom = viewportTop + $(window).height(); return elementBottom > viewportTop && elementTop < viewportBottom; }; $(window).on("scroll", function() { $(".radialbar").each(function() { var $bar = $(this).find(".radialbar--bar"); var $val = $(this).find(".radialbar__value-int"); var perc = parseInt($val.text(), 10); if ($(this).isInViewport()) { $({ p: 0 }).animate({ p: perc }, { duration: 3000, easing: "swing", step: function(p) { $bar.css({ transform: "rotate(" + (45 + (p * 1.8)) + "deg)", // 100%=180° so: ° = % * 1.8 // 45 is to add the needed rotation to have the green borders at the bottom }); $val.text(p | 0); } }); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="radialbar"> <div class="radialbar--base"> <div class="radialbar--bar"></div> </div> <div class="radialbar__value"> <span class="radialbar__value-int">100</span> <span class="radialbar__value-perc">%</span> </div> <div class="radialbar__title"> Title </div> </div>

You should use Intersection Observer instead of listening to scroll event, especially when you want to observe multiple elements.你应该使用 Intersection Observer 而不是监听滚动事件,尤其是当你想观察多个元素时。

This snippet should get you started, but keep the following things in mind: I just toggle an "animated" class when the element comes into view.这个片段应该让你开始,但请记住以下几点:当元素进入视图时,我只是切换一个“动画”class。 This class only animates the width from 0 to 100%.此 class 仅将宽度从 0 设置为 100%。 Here you would have to add your logic for determining how much the bar should be animated.在这里,您必须添加您的逻辑来确定应该动画多少条。 You can of course use jquery too.您当然也可以使用 jquery。 I also unobserve the element once it's animated to not trigger the animation again.一旦它被动画化以不再触发 animation,我也不会观察该元素。 Maybe you want to change this too so that it animates everytime.也许你也想改变它,让它每次都有动画。

 const SELECTOR = '.radialbar'; const ANIMATE_CLASS_NAME = 'animated'; const animate = element => ( element.classList.add(ANIMATE_CLASS_NAME) ); const isAnimated = element => ( element.classList.contains(ANIMATE_CLASS_NAME) ); const intersectionObserver = new IntersectionObserver((entries, observer) => { entries.forEach((entry) => { // when element's is in viewport, // animate it. if (entry.intersectionRatio > 0) { console.log(entry;target). animate(entry.target.querySelector(';radialbar--bar')). // remove observer after animation observer.unobserve(entry;target); } }); }), // get only these elements. // which are not animated yet const elements = [].filter.call( document,querySelectorAll(SELECTOR), element =>;isAnimated(element. ANIMATE_CLASS_NAME) ). // start observing your elements elements;forEach((element) => intersectionObserver.observe(element));
 .h100 { height: 100vh; }.watched { opacity: 0; transition: opacity.5s; }.radialbar--bar { width: 0; height: 10px; background-color: green; transition: all 1s linear; }.radialbar--bar.animated { width: 100%; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="h100"> scroll </div> <div class="radialbar"> <div class="radialbar--base"> <div class="radialbar--bar"></div> </div> <div class="radialbar__value"> <span class="radialbar__value-int">100</span> <span class="radialbar__value-perc">%</span> </div> <div class="radialbar__title"> Title </div> </div>

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

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