Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检测不在IE中工作的固定位置JavaScript_Javascript_Html_Css_Internet Explorer - Fatal编程技术网

检测不在IE中工作的固定位置JavaScript

检测不在IE中工作的固定位置JavaScript,javascript,html,css,internet-explorer,Javascript,Html,Css,Internet Explorer,我使用YS作为固定位置菜单,在firefox中运行良好,但在IE中不起作用 $(function(){ // this is the shorthand for document.ready $(document).scroll(function(){ // this is the scroll event for the document scrolltop = $(document).scrollTop(); // by th

我使用YS作为固定位置菜单,在firefox中运行良好,但在IE中不起作用

$(function(){                     // this is the shorthand for document.ready
  $(document).scroll(function(){    // this is the scroll event for the document

      scrolltop = $(document).scrollTop();  // by this we get the value of the scrolltop ie how much scroll has been don by user
      if(parseInt(scrolltop) >= 80)         // check if the scroll value is equal to the top of navigation
      { 
          $("#navbar").css({"position":"fixed","top":"0"});       // is yes then make the position fixed to top 0
      }
      else
      {
          $("#navbar").css({"position":"absolute","top":"80px"}); // if no then make the position to absolute and set it to 80
      }
  }
}

为ie解决此问题的任何解决方案?

为position fix解决此问题的父元素必须具有此样式

position:relative;
致以最诚挚的问候

您的代码中缺少“')(在IE7和IE9中测试)


在我看来,问题在于IE没有触发
.scroll
事件。至少,不是在JSFIDLE中。如果您显式地触发了事件,似乎确实解决了问题。它是有效的。守则:

$(function()
{
    $(document).scroll(function()
    {//add var here, avoid evil globals:
        var scrolltop = $(document).scrollTop();  
        if(parseInt(scrolltop) >= 80)         
        { 
            $("#navbar").css({"position":"fixed","top":"0"});       
        }
        else
        {
            $("#navbar").css({"position":"absolute","top":"80px"});
        }
    });//close properly
    $(document).scroll();//explicit call
});//close this, too

什么版本的IE?6,7,8,9,10?6,7,8,不必检查9:SMaybe结束文档。准备使用
})
可以帮助。仅供参考,Stackoverflow不使用pre和code标记。@MirkoMukaetov:最后两个结束括号都需要是
,同时添加
var
tp
scrolltop=$(document.scrolltop()以避免隐含全局变量。最后一点:固定元素和绝对定位元素之间有区别。这是可以的,但在IE 7,8中对我不起作用,Mozila是fine@MirkoMukaetov我改变了我的代码,它正在工作。我使用的不是文档而是窗口。检查JSFIDLE这对我来说也很有效,但是在ie中,我的菜单变成了左边距100px,我应该添加这样的元素吗,e left:0还是什么?我不知道你想让那个元素去哪里,但只需要处理CSS规则,但是改变
left
似乎也是我的下一步逻辑
$(function()
{
    $(document).scroll(function()
    {//add var here, avoid evil globals:
        var scrolltop = $(document).scrollTop();  
        if(parseInt(scrolltop) >= 80)         
        { 
            $("#navbar").css({"position":"fixed","top":"0"});       
        }
        else
        {
            $("#navbar").css({"position":"absolute","top":"80px"});
        }
    });//close properly
    $(document).scroll();//explicit call
});//close this, too