Rahul Shah
Rahul Shah

Reputation: 1407

Simple javascript does not work in any IE?

I am creating a slight minor parallax effect with this code.Everything works fine everywhere,except IE.I am using IE 9.

Jsfiddle-javascript

Jsfiddle-jquery

<div id="head"> i </div>
<div id="subHead"> can </div>
<div id="content">  haz </div>

Javascript

window.onscroll = function(ev){
  var subHead = document.getElementById('subHead'),
      topHeight = document.getElementById('head').offsetHeight;

  subHead.style.top = (topHeight - document.body.scrollTop / 4) + 'px';
};    

CSS

#head, #subHead{
    position: fixed;           
    height: 80px;    
    width: 100%;
    display: block;
    background: #c00;
    left: 0;
    top: 0;
    z-index: 10;
}

#subHead{
    z-index: 4;
    background: #cd0; 
    top: 80px;
}

#content{
    position: relative;
    z-index: 6;
    height: 1000px;
    background: #eee;
    margin-top: 160px;
}

i tried googling some cross browser tricks,but in vain...Is there any way to make it work in IE ?thanks a lot.

Edit:

Purpose: To make a 1 div move slower than another when a user scrolls.

Logic:make the div fixed via css and change its postion via javascript http://jsfiddle.net/MRbWY/11/

Error in IE.The javascript doesnt work.hence the div remains only fixed

Upvotes: 11

Views: 337

Answers (1)

Emissary
Emissary

Reputation: 10148

document.body.scrollTop was always reading zero, a bit of digging turns up this question, you want to use document.documentElement.scrollTop for IE.

updated your fiddle

Upvotes: 3

Related Questions