Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling Vertical Parallax

I'm working on a simplified vertical parallax (similar to http://nikebetterworld.com).

I've got a quick demo working - the code technically works, but I'm getting a jittery effect on the repaint after each scroll - it seems like the javascript is happening late. Any idea why? I can't see anything in the script that really stands out.

var getYPosition = function() {
  if (typeof(window.pageYOffset) == 'number') {
    return window.pageYOffset;
  } else {
    return document.documentElement.scrollTop;
  }     
};

$(document).ready(function(){
  var sections = $(".section");
  $(window).scroll(function() {
    var x = getYPosition(),
    y = Math.floor(x / 1600),
    z = $(sections[y]).offset();
    $(sections[y]).css("background-position", "0 " + (getYPosition() - z.top)/2 + "px");
  });
});
like image 450
andrewheins Avatar asked Jul 26 '11 02:07

andrewheins


People also ask

What is the parallax scrolling effect?

What is parallax scrolling? Parallax scrolling is a computer graphics technique used by web designers to create a faux-3D effect. As users scroll down a webpage, different layers of content or backgrounds move at different speeds, and this creates an optical illusion.

What is the purpose of parallax effect?

In a nutshell, it's a way to create an illusion of depth while a user scrolls down your website by making the background images move more slowly than the foreground images (we'll explain this in more detail and show you an example). Parallax effects are a way to make your web pages more dynamic and interesting.

Is parallax scrolling accessible?

A parallax effect is a design trend where background elements move at different speeds than foreground elements while scrolling. This type of motion is harmful to people with vestibular disorders and should be avoided or used with extreme caution and restraint.


1 Answers

It looks like the images are being moved twice - first by the browser scroll, and then by your scroll() handler. Hence the jitter.

You might get a cleaner effect by using position:fixed or background-attachment:fixed for the images - this way they are unaffected by the browser scroll, but will be moved by the scroll() handler. You'll no longer have one effect fighting with the other. You'll have to modify your scroll() handler accordingly.

like image 54
brainjam Avatar answered Sep 29 '22 19:09

brainjam