简体   繁体   English

jQuery Parallax / Scroll Events性能

[英]jQuery Parallax / Scroll Events Performance

This question pertains less to parallax than it does to scroll events. 这个问题与视差的关系要小于滚动事件的问题。 My overriding concern with my project is that the parallax scroll effect is smooth and not jittery. 我对项目的首要关注是视差滚动效果是平滑而不是紧张。

My question then is, do certain properties animate / scroll better than others? 那么我的问题是,某些属性是否比其他属性更好地动画/滚动? For example does background-position work better than say adjusting margin-top on scroll. 例如,背景位置比滚动时调整margin-top更好。

I'm not sure if some properties create less overhead while animating than others but I would be very interested if someone posts some good information on that subject. 我不确定某些属性是否会比其他属性创建更少的开销,但如果有人在该主题上发布了一些有用的信息,我会非常感兴趣。 That being said I do know of a couple things that can help performance. 话虽如此,我确实知道一些可以帮助提高性能的东西。

Setting position : absolute removes the element from the regular flow of the page and therefore does not require the entire page to be redrawn when it is animated. 设置position : absolute从页面的常规流中删除元素,因此在动画时不需要重绘整个页面。 position : relative will force a redraw of the whole page since ancestor and descendant elements can be affected. position : relative将强制重绘整个页面,因为祖先和后代元素可能会受到影响。

Also, you can throttle the scroll event and still achieve 30fps: 此外,您可以限制scroll事件,仍然达到30fps:

var scroll_ok = true;
setInterval(function () {
    scroll_ok = true;
}, 33);//33ms is 30fps, you can try changing this to something larger for better performance
$(window).bind('scroll', function () {
    if (scroll_ok === true) {
        scroll_ok = false;
        //now run your code to animate with respect to scroll
    }
});

UPDATE :: 2014-08-26 更新:: 2014-08-26

Things have changed since this was originally written. 自最初编写以来,情况发生了变化。 A few quick notes: 一些快速说明:

  • Modern browsers now attempt to GPU accelerate the painting of elements given a specific set of properties ( opacity and transform off the top of my head). 现代浏览器现在尝试GPU加速元素的绘制,给定一组特定的属性( opacitytransform在我的头顶)。 Using these properties can greatly improve performance over others like top / left (which also require redrawing the page in more instances than using a transform ). 使用这些属性可以大大提高性能,比如top / left (这也需要在比使用transform更多的实例中重新绘制页面)。

  • will-change is a new property just being picked up by browsers. will-change是一个刚被浏览器选中的新属性。 You can basically set a list of properties that are likely to change so the browser can optimize the property change ahead of time. 您基本上可以设置可能更改的属性列表,以便浏览器可以提前优化属性更改。

  • requestAnimationFrame has a robust polyfill and good modern browser support. requestAnimationFrame具有强大的polyfill和良好的现代浏览器支持。 This is a much better way to smoothly animate elements without creating tons of "chunk" as the browser will render as it can. 这是一种更好的方式来平滑地动画元素,而不会创建大量的“块”,因为浏览器将尽可能地渲染。

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

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