0

I'm trying to create some parallax effect.

This is how it looks the undesirable result when using 3d translations (notice the flickering of the 1st image)

enter image description here

This is quite noticeable when scrolling in a kinetic trackpad such as the MacBook ones, which do it very smoothly.

In this reduced example I'm just scrolling a background the same amount of pixels than we scroll in order to create a fixed parallax effect. I've done it this way for a better visibility of the issue, the background won't be fixed in the real case, but working in a parallax way.

Reproduction online(For some reason it requires inspecting the iframe and opening it in a new tab to remove the jsfiddle header. It works fluid within the iframe=)

The code has nothing new or extraordinary:

$(document).ready(function() {

     //polify
    (function() {
        var requestAnimationFrame = window.requestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.msRequestAnimationFrame;
        window.requestAnimationFrame = requestAnimationFrame;
    })();

    $(window).on('scroll', scrollHandler);

    var ticking = false;
    function scrollHandler(){
        if(!ticking){
            requestAnimationFrame(update);
            ticking = true;
        }
    }
    var test = $('.section').first().find('.bg');

    function update(){
        test.css({
            'transform': 'translate3d(0px, '+ $(window).scrollTop() + 'px, 0px)'
        })
        ticking = false;
    }
});
Alvaro
  • 40,778
  • 30
  • 164
  • 336
  • Not 100% on this, but I think this might be browser-specific behavior. I can see the shimmering effect in Edge, but not in Chrome. I think it has to do with the timing of the scroll event and the repaint of the window. I think some browsers are emitting the scroll event after the repaint and others are emitting it before the repaint. If the repaint occurs before your scroll event is fired, it will cause the first background to shift in the direction of the scroll, then the scroll event gets fired, then the code updates the CSS transform, then another repaint happens to position the bg. – jeffjenx Jan 16 '17 at 23:04
  • I tried changing the code to continuously poll the requestAnimFrame instead of tying it to the scroll event, to no avail. I'm also pretty sure that scroll events on iOS devices only get emitted after the scrolling has stopped, so I think an alternative solution might be required. Could be completely wrong on all of that, though. Possible related SO post: http://stackoverflow.com/a/21917142/1028949 – jeffjenx Jan 16 '17 at 23:05

0 Answers0