javascript - Hide fixed positioned div (parallax) under other divs -


i'm trying set parallax effect div inside section of page, trouble i'm having hard time positioning shows on parent section (id 'foo' on code below) of page. how can make happen?

html

<section>   <p>duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis @ vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.</p>  </section>  <article id="foo">     <div id="parallax"></div> </article>  <section>     <p>duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis @ vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.</p>  </section> 

css

#parallax {     background: url('http://lorempixel.com/output/cats-q-c-220-129-8.jpg') transparent no-repeat;     width:100%;     height:200px;     z-index: 20;     right: -73%;    position: fixed; }  article {     height: 200px;     background-color: aliceblue; }  section {     background-color: black;     color: white;     padding: 1%; } 

js

$(window).scroll(function(){  function parallax(){  if($(window).scrolltop() < $('#foo').offset().top){  var parallaxlayer = document.getelementbyid('parallax'); parallaxlayer.style.top = ((window.pageyoffset / 4) - 400)+'px'; }else{  }  } window.addeventlistener("scroll", parallax, false); }); 

http://codepen.io/anon/pen/ksahn

demo: http://jsfiddle.net/fdg9du90/show/

code: http://jsfiddle.net/fdg9du90/

var w=$(window), bg = $('.bg');  w.scroll(function(e){    bg.css('top','-'+w.scrolltop()*.5+'px'); }); 

explanation:

background image/div set fixed, jquery used make things easier mean attaching event, getting data fast , in right form, etc. on scroll event @ windows data amount window has scrolled taken , halfed , background div moved amount.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -