Javascript 透明导航条
我试着在我的网站上做一个导航条,在标题上是透明的,然后在滚动它的颜色变为某种颜色后,我写了一些js代码来达到这个目的,但是在我的例子中,我有一个小问题,当我在页面上滚动并返回到标题时,导航栏不再返回透明,仍然带有颜色,只有在启动或刷新时才是透明的Javascript 透明导航条,javascript,jquery,Javascript,Jquery,我试着在我的网站上做一个导航条,在标题上是透明的,然后在滚动它的颜色变为某种颜色后,我写了一些js代码来达到这个目的,但是在我的例子中,我有一个小问题,当我在页面上滚动并返回到标题时,导航栏不再返回透明,仍然带有颜色,只有在启动或刷新时才是透明的 <script> var x = document.getElementById("navy"); window.onscroll = function(){ if(document.body.
<script>
var x = document.getElementById("navy");
window.onscroll = function(){
if(document.body.scrollTop >= 200)
{
x.classList.add("transparent")
x.classList.remove("scrolled")
}
else
{
x.classList.add("scrolled")
x.classList.remove("transparent")
}
};
</script>
var x=document.getElementById(“海军”);
window.onscroll=函数(){
如果(document.body.scrollTop>=200)
{
x、 添加(“透明”)
x、 classList.remove(“滚动”)
}
其他的
{
x、 添加(“滚动”)
x、 classList.remove(“透明”)
}
};
首先,当您返回到标题时,您希望它是透明的,这样您的if-else语句的顺序就已经错误了
if(document.body.scrollTop >= 200)
{
x.classList.add("scrolled") //when its >= 200 it means the user is
x.classList.remove("transparent") //scrolling downwards
}
else
{
x.classList.add("transparent") //so I swapped your classes for you
x.classList.remove("scrolled")
}
其次,document.body.scrollTop并不是检查用户滚动位置的唯一方法,我发现混合使用一些命令(如skrollr的源代码中的命令)对我来说非常有效
Skrollr.prototype.getScrollTop = function() {
if(_isMobile) {
return _mobileOffset;
} else {
return window.pageYOffset || documentElement.scrollTop || body.scrollTop
|| 0;
}
};
下面是源代码上的github链接:首先,当您返回到标题时,您希望它是透明的,这样您的if-else语句的顺序就已经错误了
if(document.body.scrollTop >= 200)
{
x.classList.add("scrolled") //when its >= 200 it means the user is
x.classList.remove("transparent") //scrolling downwards
}
else
{
x.classList.add("transparent") //so I swapped your classes for you
x.classList.remove("scrolled")
}
其次,document.body.scrollTop并不是检查用户滚动位置的唯一方法,我发现混合使用一些命令(如skrollr的源代码中的命令)对我来说非常有效
Skrollr.prototype.getScrollTop = function() {
if(_isMobile) {
return _mobileOffset;
} else {
return window.pageYOffset || documentElement.scrollTop || body.scrollTop
|| 0;
}
};
下面是源代码上的github链接:
使用浏览器的开发工具在回调中添加断点,检查值并逐步检查,可能
x
是在其他地方修改的(我个人会在回调中定义x
)@arieljuod很好的提示谢谢你:)使用浏览器的开发工具在回调中添加一个断点,检查值并一步一步地检查,也许x
在其他地方被修改过(我个人会在回调中定义x
)@arieljuod好提示谢谢:)非常感谢这正是我需要的谢谢先生^非常感谢这正是我需要的谢谢先生^^