Solved

A a slow scroll (parallax) background

Posted on 2013-05-31
7
622 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now