简体   繁体   English

如何提高JS滚动性能?

[英]How to improve JS scroll performance?

I'm working on revamping my website, and the new one can be found on http://beta.namanyayg.com/ 我正在修改我的网站,新的网站可以在http://beta.namanyayg.com/找到

There are mainly two things related to scroll on the site: 网站上滚动主要有两件事:

  • To check on which 'page' the user is on, by calculating the top offset and scroll position, then adding a class to the page. 要检查用户所在的“页面”,通过计算顶部偏移和滚动位置,然后向页面添加一个类。
  • To smooth scroll on menu click. 要在菜单上平滑滚动,请单击。

I've written code for both, but there is a lot of lag . 我已为两者编写代码,但存在很多延迟

The first one almost always results in lagging. 第一个几乎总是导致滞后。 The second one, as a result, lags too. 结果,第二个也是滞后的。 I have included a boolean to check if it's smooth scrolling and disabled the normal scroll events then, but there's not much change. 我已经包含了一个布尔值来检查它是否平滑滚动并禁用了正常的滚动事件,但是没有太大的变化。

Do you have any advice on how to improve performance so there is no (or at least, less) lag? 您对如何提高性能有任何建议吗?因此没有(或至少更少)延迟? Thank you in advance! 先感谢您! :) :)

...Or is it not related to JS at all? ......或者它与JS根本没有关系? I've optimized everything else... 我已经优化了其他一切......

EDIT: Unminified JS at http://beta.namanyayg.com/js/main.js 编辑: 未经授权的JS, 网址http://beta.namanyayg.com/js/main.js

如果你使用下划线 ,它有一个很棒的_.debounce函数,非常适合这种事情。

To check how much the user has scrolled from the top of the page (ie on which 'page' he is at the moment) can be achieved with: 要检查用户从页面顶部滚动了多少(即他当前在哪个“页面”),可以通过以下方式实现:

$(window).scroll(function () { 
    var scrollAmount = $(window).scrollTop(); // in pixels

    if(scrollAmount > SOME_AMOUNT)
    {
       // add required css class
    }
});

To scroll smoothly, to some id for example, you could use: 要平滑滚动,例如某个id,您可以使用:

$("html, body").animate({ scrollTop: $("#someID").scrollTop() }, 1000);

These are both jQuery solutions, so you should have jquery library included. 这些都是jQuery解决方案,所以你应该包含jquery库。 There is also a nice jQuery plugin called waypoints that performs these calculations. 还有一个很好的jQuery插件叫做路点 ,可以执行这些计算。 It might prove useful to you and it has some other nice features and examples. 它可能对您有用,它还有一些其他不错的功能和示例。

I have the same problem. 我也有同样的问题。 I have a scrollable div with thousands of smaller divs. 我有一个可滚动的div,有数千个较小的div。 Every time I call scrollTop to get the scroll-position or set it, it sometimes waits at least 1 second. 每次我调用scrollTop来获取滚动位置或设置它时,它有时会等待至少1秒钟。

I read these slides: http://www.slideshare.net/nzakas/high-performance-javascript-2011 (especially slides 138-139) and now I realize that every call to scrollTop, even as a getter, makes javascript relayout the page. 我读了这些幻灯片: http ://www.slideshare.net/nzakas/high-performance-javascript-2011(特别是幻灯片138-139),现在我意识到每次调用scrollTop,即使是作为一个getter,也会让javascript重新启动页。 This is most likely the cause of delay, but unfortunately I have not found a solution yet, as in a way to call scrollTop without causing relayouts. 这很可能是延迟的原因,但不幸的是我还没有找到解决方案,因为在调用scrollTop的同时不会导致重新布局。

Note: I've only been testing on Chrome. 注意:我只在Chrome上测试过。

Also read 'Browsers are smart' section of this article: http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/ 另请阅读本文的“浏览器很聪明”部分: http//www.phpied.com/rendering-repaint-reflowrelayout-restyle/

I've found an easy solution to the lag with getting scrollTop, just call it inside a scroll-handler and save the result in a variable. 通过获取scrollTop,我找到了一个简单的滞后解决方案,只需在滚动处理程序中调用它并将结果保存在变量中。

for example in jQuery: 例如在jQuery中:

var scrollPos = 0,
    element = $('.class');

element.scroll(function(){
    scrollPos = element.scrollTop();  
});

For the second problem, setting the scrollTop, I reduced the amount of DOM elements by only showing the visible elements. 对于第二个问题,设置scrollTop,我通过仅显示可见元素来减少DOM元素的数量。 In your case make sure only the visible page(s) are added to the DOM. 在您的情况下,请确保仅将可见页面添加到DOM。 when scrolling to the next page, in the scroll handler remove the top one (use jQuery .detach) and append the next one to the DOM. 当滚动到下一页时,在滚动处理程序中删除顶部的一个(使用jQuery .detach)并将下一个附加到DOM。

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

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