0

Goal

This is a follow-up question to this one.

  1. I'm trying to create a parallax scrolling effect.

  2. The parallax-container are implemented like so:

    < div class="parallax slide-1" >< /div >

The parallax-effect successfully starts, when a container is scrolled into view and stops, when it has left the view.

Problem

Unfortunately when I place a parallax-container further down on a page the effect starts with the backgroundposition in the wrong place.

I understand why this problem shows up, but am not sure how to solve it.

What I need is basically this:

  1. Start the effect once a parallax-container is scrolled into view: works.
  2. Stop the effect once it has left the view: works.
  3. Only move the backgroundposition by the distance scrolled since it has entered the view. Not the distance scrolled relative to the top of the page.

Fiddle over here

For the parallax-container further down the page:

You can see the edges of the images. I'm trying to find a solution for that.

Thoughts

So far my attempts where based around the idea to get the distance of a parallax-container to the top of the page only once (dont update it with each scroll) and implement it in the calculations.

But I cant seem to get it to work properly. Am I missing something?

Some further clarification:

  1. There can be multiple parallax-container on any page.

  2. Each can have their own background image set. (in the css)

  3. I dont know how many parallax-container will be on a page.

  4. I dont know where they are placed.

  5. Only those that are visible move.

Code

Only the relevant parts:

$(window).scroll(function(){ // Bind window scroll event
    $( ".parallax" ).each(function() {
        if( $( this ).is_on_screen() ) { // Check if element is visible on screen after DOM loaded

            // ANIMATE PARALLAX EFFECT
            // If Parallax Element is scrolled into view do...

            // Variables
                var speed     = 2.5;
                var calc      = (-window.pageXOffset / speed) + "px " + (-window.pageYOffset / speed) + "px";
                var container = $( this );

            // Function
                container.css({backgroundPosition: calc});

        } else {
            // ...otherwise do nothing
        }
    });
});
Community
  • 1
  • 1
Arrowcatch
  • 1,612
  • 3
  • 19
  • 26

1 Answers1

0

I was actually going to put an edit on my old post, but since you made a new one, I'll add it here.

Seeing as you want to get granular with your parallax (ie. 2 are in view, but 1 has only moved 10px vs 30px), what you could do is store the speed option inside of your object, so that way, the speed movement will become independant. As so:

$(window).scroll(function(){ // Bind window scroll event
    $( ".parallax" ).each(function() {
        if( $( this ).is_on_screen() ) { // Check if element is visible on screen after DOM loaded

            // ANIMATE PARALLAX EFFECT
            // If Parallax Element is scrolled into view do...
            // set a starting speed for each this:
            // $(this).speed = 2.5;
            //
            // Variables

                var calc      = (-window.pageXOffset / $(this).speed) + "px " + (-window.pageYOffset / $(this).speed) + "px";
                var container = $( this );

            // Function
                container.css({backgroundPosition: calc});

        } else {
            // ...otherwise do nothing
        }
    });
});

Edit: As discussed in the comments, to set the speed, try the following:

$(document).ready(function () {    
    $.fn.is_on_screen = function () {
        //..do your on screen stuff
    };
    $( ".parallax" ).each(function() {
       $(this).speed = 2.5;
    };
});
Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
  • Hi Dylan, many thanks for reaching out again! Huge props. As far as I understand your approach would require me to manually set a speed for each parallax-container I place on a page, right? I am looking for a solution where this is not neccessary. I've created [a fiddle over here](http://jsfiddle.net/pG5vy/3/) with the original code. Basically I want to get rid of the fact that you can see the image edges for parallax-container further down the page. – Arrowcatch Jun 19 '14 at 13:30
  • You could make a .each loop (like the one above) in your document.ready function, and set each speed... Besides that, I wanna make sure i'm getting things right though. When you are scrolling around on the page, you want to make it so there is only one image to a page? or were you looking to have it so you have both images, but only the most predominant one moves? or? – Dylan Corriveau Jun 19 '14 at 14:03
  • Ah interesting idea with the additional .each loops. I'll give it a try. For clarifications: There can be multiple parallax-container on any page. Each can have their own background image set. I dont know how many parallax-container will be in a page or where they are placed. Only those that are visible move. Thanks a lot for your help so far! – Arrowcatch Jun 19 '14 at 14:27
  • Not a problem. Thanks for the clarification. I will add an edit for the initial .each in just a sec – Dylan Corriveau Jun 19 '14 at 14:32
  • Hmmm... nope, I cant seem to be getting it to work :-( [Heres a Fiddle](http://jsfiddle.net/Gw8FV/1/) – Arrowcatch Jun 19 '14 at 15:20