0

I am a noob when it comes to jQuery so please be patient with me :-)

I have created (with some internet help) a parallax page.
The content of that page get's loaded dynamically from a WordPress environment.

The problem is the following:
In my example I use three pages. Two with a background and one without
1. Background
2. No background
3. Background

The problem is that the third page that contains a background starts scrolling while the viewport isn't even visible.
So by the time the user sees the third part, most part of the image is allready gone.

Hope I make some sense.

Here is the link of the tut I followed

I found that:
$('.my_class').parallax("50%", 0.2); handles the speed of the scrolling.

If I tune it up it will work for three pages but the effect is almost gone...

This dynamically loads the page:

<?php $args = array(
    'sort_order' => 'asc',
    'sort_column' => 'menu_order',
    'child_of' => $post->ID,
    'post_type' => 'page',
    'post_status' => 'publish'
    ); 
    $pages = get_pages($args); 
    foreach ($pages as $page){
        $thumbnail_object = get_post(get_post_thumbnail_id($page->ID));
        echo '
        <div class="my_class" style="background:url('.$thumbnail_object->guid.') no-repeat center center fixed;-webkit-background-size: cover; -moz-background-size: cover;  -o-background-size: cover; background-size: cover;margin: 0 0 5% 0;">
        <div class="wrap_1280">';
        echo apply_filters( 'the_content', $page->post_content );
    echo '</div>    
    </div>';

    }

?>

I load the background image inline because that way I can dynamically change the image to whatever the user uploads.

Any ideas how I can fix this?

---------- UPDATE-----------
Here is what I placed in the body tag

<script>
function isElementInViewport (el) {

//special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
    el = el[0];
}

var rect = el.getBoundingClientRect();

return (
    rect.top >= 0 &&
    rect.left >= 0 &&
    rect.bottom <= (window.innerHeight ||   document.documentElement.clientHeight) && /*or $(window).height() */
    rect.right <= (window.innerWidth || document.documentElement.clientWidth)   /*or $(window).width() */
);
}
function onVisibilityChange(el, callback) {
var old_visible;
return function () {
    var visible = isElementInViewport(el);
    if (visible != old_visible) {
        old_visible = visible;
        if (typeof callback == 'function') {
            callback();
        }
    }
}
}

var handler = onVisibilityChange(el, function() {


jQuery('.full_width_parallax_scroll').parallax("50%", 0.8);

jQuery(window).scroll(function() {
if($(this).scrollTop() > 150) {
    if(!$('main-menu').hasClass('floating')) {
       $('.main-menu').addClass('floating');
    }
} else {
    jQuery('.main-menu').removeClass('floating');
}
});

});


//jQuery
$(window).on('DOMContentLoaded load resize scroll', handler); 

/* //non-jQuery
if (window.addEventListener) {
addEventListener('DOMContentLoaded', handler, false); 
addEventListener('load', handler, false); 
addEventListener('scroll', handler, false); 
addEventListener('resize', handler, false); 
} else if (window.attachEvent)  {
attachEvent('onDOMContentLoaded', handler); // IE9+ :(
attachEvent('onload', handler);
attachEvent('onscroll', handler);
attachEvent('onresize', handler);
}
*/
</script>
Interactive
  • 1,336
  • 3
  • 22
  • 45
  • Suggestion for more research: look into detecting when an element is visible in the viewport, wrap the second parallax code in a function that checks for it, and it won't happen until whatever element you pick is visible on the page (e.g. has been scrolled into view): http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport/7557433#7557433 or http://stackoverflow.com/questions/20791374/jquery-check-if-element-is-visible-in-viewport – TylerH Jan 21 '16 at 16:44
  • Thnx. Don't really understand it but if I place the code without reading :-) I get `el undefined` – Interactive Jan 21 '16 at 16:51
  • Okay so now I have read it and came across a part that said: /* your code go here */. *sigh* So I placed the entire `scrollcode` there and now it kinda works. The background images don't move anymore.... Make sense? See updated question! – Interactive Jan 21 '16 at 16:55

0 Answers0