简体   繁体   English

提高背景视差滚动的性能

[英]Improving Performance on Background Parallax Scrolling

Hello StackOverflow Community, 你好StackOverflow社区,

what I am trying to achieve is a header that can be moved with the mouse. 我想要实现的是一个可以用鼠标移动的标题。 You klick into the header and drag the mouse and the elements inside the header will move with different speeds. 您可以轻触标题并拖动鼠标,标题内的元素将以不同的速度移动。

I achieved the parallaxing part but the performance is not really good. 我实现了parallaxing部分,但性能并不是很好。 It is partially a bit laggy while dragging the backgrounds. 拖动背景时有点滞后。

My question now is: what can be changed in the code to get a performance boost? 我现在的问题是:在代码中可以改变什么才能获得性能提升?

That's the part of the code that takes care of parallaxing. 这是处理parallaxing的代码的一部分。 On every mousemove a each loop is executed which I think is the reason for the performance beeing so laggy: 在每个mousemove上执行每个循环,我认为这是性能如此滞后的原因:

var dragging = false;
var clickMouseX;

//Our object for the layers
//each layer has a different scrolling speed
var movingObjects = {
    '#header-l1' : {'speed': 1},
    '#header-l2' : {'speed': 1.4},
    '#header-l3' : {'speed': 1.85},
    '#header-l4' : {'speed': 2.2},
};

$('#header-wrapper').mousedown(function(e){
    dragging = true;

    //Get initial mouse position when clicked
    clickMouseX = e.pageX;

        $(this).mousemove(function(mme){
            //execute only if mousedown
            if(dragging){
                //iterate through all layers which have to be parallaxed
                $.each(movingObjects, function(el, opt){
                    var element = $(el);
                    //get difference of initial mouse position and current mouse position
                    var diff = clickMouseX - mme.pageX;
                    //scroll-position left speed 1
                    if(diff < 0) diff = -1;
                    //scroll position right speed 1
                    if(diff >= 0) diff = 1;
                    //get current position of layer
                    currLeft = parseInt(element.css('left'));
                    //get current layer width
                    elWidth = element.width();

                    //if right border is reached don't scroll further
                    if(currLeft < -(elWidth - 810)){
                        element.css('left', -(elWidth - 810));
                    }           
                    //so do with left border
                    if(currLeft > 0){
                        element.css('left', 0);
                    }
                    //parallax it! Subtract the scroll position speed multiplied by the speed of the desired
                    //layer from the current left property
                    element.css('left', parseInt(element.css('left')) - diff*opt.speed);    
                });
            }
        });


    /* Cursor */
    $(this).css('cursor', 'pointer');
    return false;
});

I also put a fiddle up: http://jsfiddle.net/yWGDz/ 我也放了一个小提琴: http//jsfiddle.net/yWGDz/

Thanks in advance, Thomas 托马斯先谢谢你

PS maybe someone even finds out why layer two and three have the same scroll speed while having different speeds defined. PS也许有人甚至会发现为什么第二层和第三层具有相同的滚动速度,同时定义了不同的速度。

I worked at this a bit, and came up with this: http://jsfiddle.net/amqER/2/ 我在这方面工作了一下,想出了这个: http//jsfiddle.net/amqER/2/

This works a lot faster than the original (especially in firefox, where it performs a whole lot better, chrome it's still pretty slow). 这比原版快得多(特别是在firefox中,它表现得更好,Chrome它仍然很慢)。 I also changed up some of the logic in your code, to make it make more sense. 我还改变了代码中的一些逻辑,使其更有意义。

A list of things that I did: 我做过的事情清单:

Minify your pngs 缩小你的朋友

2 of your png files were over 2 megs, so I threw them into a png compressor ( tinypng ) and that reduced the size a lot. 你的2个png文件超过2兆,所以我把它们扔进了png压缩器( tinypng ),这大大减少了。 This helps with loading time and overall snappiness. 这有助于加载时间和整体快速。

Re-use values as much as possible 尽可能重复使用值

In your original code, you wrote to and then subsequently read from the css left property a couple times in your code. 在您的原始代码中,您编写了代码,然后在css left属性中读取了几次代码。 Doing this is going to make it a lot slower. 这样做会使速度慢很多。 Instead, I kept an left property, and would only touch $.css when I absolutely needed to. 相反,我保留了一个左侧属性,并且在我绝对需要时只会触及$.css Likewise for reading each element's width each update. 同样,每次更新时读取每个元素的宽度。

Also, like I said, I modified your logic to (I think) make more sense, given what you were trying to accomplish. 另外,就像我说的那样,我修改了你的逻辑(我认为)更有意义,考虑到你想要完成的事情。 It calculates a new diff each update, and tries to move according to that. 它计算每个更新的新差异,并尝试根据它移动。 Also, it doesn't try to keep moving once one of the images falls off (which yours does if you move all the way to the right, and it looks really weird). 此外,一旦其中一个图像掉落,它就不会继续移动(如果你一直向右移动,那么它就会继续移动,看起来很奇怪)。 You can also look at this: http://jsfiddle.net/amqER/5/ , which maybe is more like the control scheme you wanted. 你也可以看看这个: http//jsfiddle.net/amqER/5/ ,这可能更像你想要的控制方案。

Just some quick performance tips. 只是一些快速的性能提示。

Try not to use $(this).mousemove instead save $(this) into a variable and use that. 尽量不要使用$(this).mousemove而是将$(this)保存到变量中并使用它。

var th = $(this);
th.mousemove...

Try to avoid using $.each. 尽量避免使用$ .each。 This is probably the part that's slowing your code down. 这可能是减慢代码速度的部分。 You can replace it with a for loop, but I would suggest, in this case, sending in each element one by one. 你可以用for循环替换它,但我建议,在这种情况下,逐个发送每个元素。

var parallax = function(img){

};

parallax(img1);
parallax(img2);

instantly-increase-your-jquery-performance 瞬间增-您-jQuery的性能

Whilst Xymostech's answer does greatly improve upon the original poster's original code; 虽然Xymostech的答案确实大大改善了原始海报的原始代码; the performance is hardly improved for me in Chrome. Chrome中的性能几乎没有提升。

Whilst inspecting the page FPS, the solution posted here runs at 15FPS for me on a Retina MacBook Pro. 在检查页面FPS的同时,这里发布的解决方案在Retina MacBook Pro上以15FPS运行。

I made a very simple change to the code, altering it to use translate3d properties instead of left . 我对代码进行了非常简单的更改,将其更改为使用translate3d属性而不是left Now, it runs at 55-60 FPS for me. 现在,它的运行速度为55-60 FPS。 I'd call that a massive performance boost. 我称这是一次巨大的性能提升。

If 'show paint rectangles' are turned on in Chrome, you'll see the previously posted solution is continually painting changes to the dom whilst the parallax is in motion. 如果在Chrome中打开“显示绘制矩形”,您将看到之前发布的解决方案是在视差处于运动状态时不断地绘制对dom的更改。 With the translate3d solution, there's simply zero painting done the whole time the parallax is in motion. 使用translate3d解决方案,在视差运动的整个过程中完成零绘画。

http://jsfiddle.net/LG47e/ http://jsfiddle.net/LG47e/

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

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