Solved

A a slow scroll (parallax) background

Posted on 2013-05-31
7
629 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

856 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