检测不在IE中工作的固定位置JavaScript
我使用YS作为固定位置菜单,在firefox中运行良好,但在IE中不起作用检测不在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
$(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
tpscrolltop=$(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