Solved

A a slow scroll (parallax) background

Posted on 2013-05-31
7
628 Views
Last Modified: 2013-06-01
Hello Experts,

I have a background that I would like to Parallax scroll (slow scroll) as the visitor scrolls up and down the page.

The background image is 1200px in height (800px in width) and I want the scroll speed to adjust based on the page content (Document) size. In other words I want the visitor to be able to see (eventually) the full background image as they scroll down seeing the bottom of the image when they hit the footer.

I've tried variosu differe methods to no avail, the current js code i have is:

function calcParallax(tileheight, speedratio, scrollposition) {
  return ((tileheight) - (Math.floor(scrollposition / speedratio) % (tileheight+1)));
}

window.onload = function() {

  window.onscroll = function() {
    var posY = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : window.pageYOffset;
    
    var ground = document.getElementById('bg');
    var groundparallax = calcParallax(53, 8, posY);
    ground.style.backgroundPosition = "0 " + groundparallax + "px"; 

  }
}

Open in new window


HTML code:

<div id="bg" class="movable" >
...all content code...
</div>

Open in new window


CSS code:

#bg {
	background:url("../images/cave-background.jpg") no-repeat fixed 50% 0;
	background-repeat: no-repeat;
 	background-attachment: fixed;
}

Open in new window


Currently the JS code does not even work :( It does not even move the bg image...

Insight would be great!

Thanks,

Morgan
0
Comment
Question by:Morgan
  • 4
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39212186
I'm not sure what the tiling is meant to do but just doing that slow scroll from top to bottom can be achieved like this:
window.onload = function() {

  window.onscroll = function() {
    var posY = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : window.pageYOffset;
    posY = posY || 0; // undefined when scrolling to the top
    var lenY = (document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : window.innerHeight;
    var ground = document.getElementById('bg');
    var groundparallax = posY * (1200 - document.documentElement.clientHeight) / (lenY - document.documentElement.clientHeight);
    ground.style.backgroundPosition = "50% -" + groundparallax + "px";

  }
  window.onscroll(); // reset position after refresh
}

Open in new window

You can see it in action here: http://schutt.nl/ee/Q_28144587/ 

I tested it in IE8 (yeah I know, sorry), FireFox and Chrome. Newer IE's (and/or if you don't use doctype html) may need other window/document properties for innerHeight and such.

EDIT: you may need an onresize event handler as well to reposition.
0
 
LVL 1

Author Comment

by:Morgan
ID: 39212931
Hello robert_schutt,

Thanks for the help. The code didn't work. You can see it here: http://testingtesting1.info/jutwynne/
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39212959
There's a load of other scripts that probably interfere, you could try using the code that is already there so instead of setting window.onload in the scroll code add it to your existing code:
<script type="text/javascript">
window.addEvent('load', function() {
				new JCaption('img.caption');
    // try adding the onscroll function here?
			});
  </script>

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39212965
By the way, if the reference to /js/background.js is supposed to contain the code, it doesn't seem to exist.
0
 
LVL 1

Author Comment

by:Morgan
ID: 39213038
Hello Robert_schutt,

I got it to work (somewhat). The issue wasn't the loading of the other scripts, but the loading of our script. The src path wasn't working (js/background.js) so I had to use an explicit path declaration.

So we are about 90% there.

If you return to the site (listed above) you will see the background does move. However, as you scroll down the footer does not scroll over it and if you go to another page where the content is longer the image gets chopped off at the bottom as you scroll down. As though a black image is moving up as you move down...its strange.

Any ideas?

Thanks,

Morgan
0
 
LVL 1

Author Comment

by:Morgan
ID: 39213066
I got it.

The issue was I had one to many end div tags...

Been looking at to much code.

Thank you so much for your help!
0
 
LVL 1

Author Closing Comment

by:Morgan
ID: 39213068
Again thank you!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question