[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

A a slow scroll (parallax) background

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
Morgan
Asked:
Morgan
  • 4
  • 3
1 Solution
 
Robert SchuttSoftware EngineerCommented:
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
 
MorganAuthor Commented:
Hello robert_schutt,

Thanks for the help. The code didn't work. You can see it here: http://testingtesting1.info/jutwynne/
0
 
Robert SchuttSoftware EngineerCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Robert SchuttSoftware EngineerCommented:
By the way, if the reference to /js/background.js is supposed to contain the code, it doesn't seem to exist.
0
 
MorganAuthor Commented:
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
 
MorganAuthor Commented:
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
 
MorganAuthor Commented:
Again thank you!
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now