Parallax effect with Stellar.js and iScroll on iOS, bounces back and stuck on certain scrollTop after scroll

1,067 views
Skip to first unread message

Mauro Julián Fernández

unread,
Jun 13, 2013, 8:00:33 PM6/13/13
to isc...@googlegroups.com

I'm developing a web for a client who wants a parallax effect applied to all the foreground elements (no background parallax). This web is 1234px width by like 9000px in height (all images, big background images and PNG with transparencies for the foreground elements) and has a couple of foreground elements. I've achieved this effect using stellar.js with iscroll.js to create a new scrollbar based on iOS. The parallax is smooth and simple, you scroll down, the elements go up and viceversa, but the problem I have (I already contacted the stellar.js developer but I'm still waiting for an answer, hope he see this :) ) is that I've got that bouncy effect that in some situations it stucks my viewport at a certain scrollTop. Lets say, I load the web and everything is ok, but suddenly I go to the top of the page for example (it happens at any scrollTop), it bounces and shows the "checkerboard" like iOS, and then it stops at like a couple of pixels down from the top, when I try to scroll up from there, it bounces back to that location.

I found some posts with this problem searching at Stack Overflow, and the answer that helped me in some way (from start I've got this problem but I couldn't even scroll down) is this one:

iScroll Not scrolling content, just bounces back to top

That post describes the same problem I have with stellar/iscroll, and changing those CSS properties helped me to minimize the bounce, but it still happens. I tried a lot of height combinations to the#wrapper #scroller elements, but following that answer means that my #scroller is like a lot more taller than the content, so when I scroll down I see this huge black background, until it bounces back to the bottom of the content.

I'll provide you with some code, can't share the web because it's not finished and my client asked me to not share it.

This is in the <head>, here I initialize the plugin, I took the template from the stellar.js tutorial on mobile parallax (http://markdalgleish.com/2012/10/mobile-parallax-with-stellar-js/):

<meta name="viewport" content="width=device-width, initial-scale=0.62, maximum-scale=0.62, minimum-scale=0.62, user-scalable=no">

<script src="js/vendor/jquery-1.9.1.min.js"></script>
<script src="js/vendor/iscroll.js"></script>
<script src="js/vendor/jquery.stellar.js"></script>
<script type="text/javascript">

    function loaded() {
      var ua = navigator.userAgent,
        isMobileWebkit = /WebKit/.test(ua) && /Mobile/.test(ua);

      if (isMobileWebkit) {
        $('html').addClass('mobile');
      }

      $(function loaded(){
        var myScroll;

        if (isMobileWebkit) {

            setTimeout(function () { myScroll = new iScroll('wrapper', { useTransform: true, useTransition: false } ); }, 5000);

            setTimeout(function () { myScroll.refresh() }, 10000);

          $('#scroller').stellar({
            scrollProperty: 'transform',
            positionProperty: 'transform',
          });
        } else {
          $.stellar({
            horizontalScrolling: false,
          });
        }
      });

    };

    document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 100); }, false);
    document.addEventListener('DOMContentLoaded', loaded, false);

</script>

<body onload="loaded()">
<div id="wrapper">
    <div id="scroller">
        <div id="fg" data-role="content">

            <!-- all the backgrounds, I load them with the js preloader and apply them as a background-image -->
            <div id='rff-bg0'></div>
            <div id='rff-bg1'></div>
            <div id='rff-bg2'></div>
            <div id='rff-bg3'></div>
            <div id='rff-bg4'></div>

            <div id='nav-bar' data-position="fixed"> <-- This nav-bar has to be fixed because is used to navigate the web
                <!--some buttons here-->
            </div>

            <!-- lots of divs, these are the foreground parallax elements, all with the data-stellar-ratio="", this is working great -->

        </div>
    </div>
</div>
</body>

CSS, I'll share the specific parts that affect the parallax, both stellar and iscroll plugins, this snippet was also taken from the stellar tutorial mentioned above:

.mobile body {
  overflow: hidden;
}

.mobile #wrapper {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 9052px;
  z-index: -1;
  background-color: #000;
}

.mobile #scroller {
    height: 20000px;
    background-color: #000;
}

Matteo Spinelli

unread,
Jun 14, 2013, 3:32:30 AM6/14/13
to isc...@googlegroups.com
I would try iScroll 5 which already has parallax scrolling integrated. Apart from that if the scroller bounces back is because of:

1) the content was not 100% available at the moment of iscroll initialization. Delay iscroll init or better send a refresh() once all content is loaded (that includes images if they don't have explicit width/height)

OR

2) there's an error in your CSS which makes iscroll think that the scroller is smaller than what it actually is. use chrome debug console and inspect the elements bounding boxes to find out the culprit.

Cheers
Matteo

Mauro Julián Fernández

unread,
Jun 14, 2013, 4:29:14 PM6/14/13
to isc...@googlegroups.com
Thank you Matteo, I'll try iScroll 5 first for the new parallax feature and tell you if it works.

Mauro Julián Fernández

unread,
Jun 15, 2013, 11:31:53 AM6/15/13
to isc...@googlegroups.com
I tried iScroll5 using the parallax feature but it doesn't work, I then realized by deactivating js files, that jQuery is the problem, when I disable jQuery, iScroll loads fine, is this a known problem? I need jQuery to manage the image preloader.

Matteo Spinelli

unread,
Jun 16, 2013, 4:06:50 AM6/16/13
to isc...@googlegroups.com
I don't see why it shouldn't work with jquery. I'd need to see a demo page.

Mauro Julián Fernández

unread,
Jun 20, 2013, 6:23:28 PM6/20/13
to isc...@googlegroups.com
I fixed it, it was some jQuery from my code. Also I fixed the scroll and I'm still using Stellar.js for the parallax, somehow it didn't worked with the one from iScroll.

I will post the site when it's finished, thanks for your work Matteo, iScroll5 is amazing.
Reply all
Reply to author
Forward
0 new messages