2

我将 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
      }
  }
}

有什么解决方案可以解决这个问题吗?

4

3 回答 3

1

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

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

      scrolltop = $(window).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
      }
  }); //here
});//here
于 2012-10-09T13:14:37.537 回答
1

对我来说,问题似乎是 IE 没有触发.scroll事件。至少,不在 jsfiddle 中。如果您明确触发该事件,那似乎确实可以解决问题。这个小提琴在 IE8 中进行了测试,并且可以正常工作。编码:

$(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
于 2012-10-09T13:40:34.573 回答
0

对于位置修复,您的父元素必须具有此样式

position:relative;

最好的祝福

于 2012-10-09T13:08:00.593 回答