繁体   English   中英

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

[英]Simple javascript does not work in any IE?

我正在用这个代码创建一个轻微的视差效果。除了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仍然是固定的

document.body.scrollTop总是读零,有点挖掘出现这个问题 ,你想使用document.documentElement.scrollTop for IE。

更新你的小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM