【问题标题】:Simple javascript does not work in any IE?简单的 javascript 在任何 IE 中都不起作用?
【发布时间】:2013-02-19 11:23:56
【问题描述】:

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

【问题讨论】:

  • 在 IE 的开发者控制台中遇到什么错误?
  • 究竟是什么不工作?
  • 在IE中,“can”这个词是固定的......它不应该是javascript的。
  • 最新版本不明确。 Win8上是IE10,Win7上是IE9。
  • @Zeta - 实际上,IE10 现在可用于 Win7。不过,目前大部分人还是会使用 IE9。

标签: javascript html css internet-explorer


【解决方案1】:

document.body.scrollTop 的读数总是为零,有点挖掘出现了this question,您想将document.documentElement.scrollTop 用于 IE。

updated your fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多