0

I trying to create a basic parallax scrolling example with text and color; no images. However the effect doesn't seem to work. Here is the code:

<!doctype html>
<html>
<head>
<title>Parallax Scrolling</title>
</head>
<style type="text/css">
body{
margin:0;
padding:0;
} 
#img1 { 
background: rgb(44,89,238) 50% 0 repeat fixed; 
height: 1000px;  
margin: 0 auto; 
width: 100%; 
max-width: 1920px; 
position: relative; 
box-shadow: 0 0 50px rgba(0,0,0,0.8);
}
#img2 { 
background: rgb(252,151,20) 50% 0 repeat fixed; 
height: 1000px;
margin: 0 auto; 
width: 100%; 
max-width: 1920px; 
position: relative; 
box-shadow: 0 0 50px rgba(0,0,0,0.8);
}
p {
height: 458px;
position: absolute;
top: 291px;
width: 100%;
font-size: 100px;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

$(document).ready(function(){


     var $window = $(window); 

     $('section[data-type="background"]').each(function(){
     var $bgobj = $(this); // assigning the object
     $(window).scroll(function() {
     var yPos = -($window.scrollTop() / $bgobj.data('speed'));
     var coords = '50% '+ yPos + 'px';
     $bgobj.css({ backgroundPosition: coords });
     });
     });
     });

</script>
<body>
<section id="img1"><p>Hello</p></section>
<section id="img2"><p>Hello</p></section>
</body>
</html>

I have tried to edit the css but nothing seems to be working. Please help.

1 Answer 1

3

There are some handy Parallax scrolling JS libs out there they might help quite a bit, take a look:

Scrolling Effect:

http://markdalgleish.com/projects/stellar.js/

Stellar is helpful and easy, it can be as simple as adding data-stellar-ratio="" to a element, but it can be adjusted with jQuery, it is writen in jQuery, so jQuery will be needed in order to user it.


Gyroscope / Mouse movement Effect:

http://wagerfield.github.io/parallax/

Parallax.js is handy as well, and also very cool, although this isn't a Parallax scrolling like you mentioned, I think you would still like it. Parallax is written in jQuery and JavaScript, so jQuery is not needed.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you know what I need to do to fix my problem?
@android211 it would be silly to make your own when there is already 2 really good ones listed right there.
@android211 Take a look at them, I think you'll like it. They are completely customizable.
I do like your suggestions but i want to know how to make my example of parallax scrolling work. Something in the code perhaps?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.