简单的JavaScript在任何IE中都不起作用?

时间:2013-03-05 06:39:39

标签: javascript html css internet-explorer

我正在使用此代码创建一个轻微的视差效果。除IE之外,其他任何地方都可以正常工作。我正在使用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;
}

我尝试使用谷歌搜索一些跨浏览器的技巧,但是徒劳无功......有没有办法让它在IE中运行?非常感谢。

修改

目的:当用户滚动时,使1 div比另一个慢。

逻辑:通过css修复div并通过javascript更改其位置 http://jsfiddle.net/MRbWY/11/

IE中的错误.javascript无法正常工作。因此div仍然只是固定

1 个答案:

答案 0 :(得分:3)

document.body.scrollTop总是读零,有点挖掘出现this question,你想用document.documentElement.scrollTop进行IE浏览。

updated your fiddle