Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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
Javascript 使用jQuery在页面顶部删除类_Javascript_Html_Jquery_Css - Fatal编程技术网

Javascript 使用jQuery在页面顶部删除类

Javascript 使用jQuery在页面顶部删除类,javascript,html,jquery,css,Javascript,Html,Jquery,Css,我正在尝试使用jQuery从我的粘性nav中删除一个类。该类是在向下滚动页面并到达某些部分(标记)后添加的 目前,行为与我编写的代码的预期一致。CSS在用户使用相应的ID滚动特定部分后应用。但是,当用户一直滚动到顶部时,CSS仍然应用于nav。当用户不查看导航中的某个部分时,我希望删除该类 感谢您的帮助 //将活动类添加到滚动导航中的链接 $(文档).ready(函数(){ $(窗口)。滚动(函数(){ var y=$(this.scrollTop(); $('.navLink')。每个(函数

我正在尝试使用jQuery从我的粘性nav中删除一个类。该类是在向下滚动页面并到达某些部分(标记)后添加的

目前,行为与我编写的代码的预期一致。CSS在用户使用相应的ID滚动特定部分后应用。但是,当用户一直滚动到顶部时,CSS仍然应用于nav。当用户不查看导航中的某个部分时,我希望删除该类

感谢您的帮助

//将活动类添加到滚动导航中的链接
$(文档).ready(函数(){
$(窗口)。滚动(函数(){
var y=$(this.scrollTop();
$('.navLink')。每个(函数(事件){
如果(y>=$($(this.attr('href')).offset().top-40){
$('.navLink')。不是(this.removeClass('navLinkActive');
$(this.addClass('navLinkActive');
}
});
});
});
//平滑滚动
jQuery($=>{
//滚动的速度(以毫秒为单位)
恒速=1000;
$('a[href*=“#”]”)
.filter((i,a)=>a.getAttribute('href').startsWith('#')|| a.href.startsWith(`${location.href}#`))
.unbind('click.smoothScroll')
.bind('click.smoothScroll',事件=>{
const targetId=event.currentTarget.getAttribute('href').split('#')[1];
const targetElement=document.getElementById(targetId);
if(targetElement){
event.preventDefault();
$('html,body')。设置动画({
scrollTop:$(targetElement).offset().top
},速度);
}
});
});
正文{
最小高度:300vh;
}
.粘的{
位置:固定;
排名:0;
左:0;
宽度:100%;
高度:60px;
z指数:10;
}
.导航链接{
颜色:#000;
}
.navLinkActive{
颜色:#FF0000;
}
.科{
高度:50vh;
}
#非导航部分{
最小高度:计算(100vh-60px);
}

与以下章节无关的内容

知识是一种美德,是一种美德,是一种美德,是一种美德

坐在阿梅特·康塞特图尔·阿利奎姆面前,告别精英


Leo a diam sollicitudin temporal id eu nisl.

我猜您只需要在滚动处理程序中使用
else
子句,可能类似于:

$(document).ready(function() {
  $(window).scroll(function() {
    var y = $(this).scrollTop();
    $('.navLink').each(function(event) {
      if (y >= $($(this).attr('href')).offset().top - 40) {
        $('.navLink').not(this).removeClass('navLinkActive');
        $(this).addClass('navLinkActive');
      } else {
        $(this).removeClass('navLinkActive');
      }
    });
  });
});

在块
else{…}
中添加从
this
中删除活动类的操作。像这样:

else {
    $(this).removeClass("navLinkActive");
}
//将活动类添加到滚动导航中的链接
$(文档).ready(函数(){
$(窗口)。滚动(函数(){
var y=$(this.scrollTop();
$(“.navLink”)。每个(函数(事件){
如果(y>=$($(this.attr(“href”)).offset().top-40){
$(“.navLink”).not(this).removeClass(“navLinkActive”);
$(此).addClass(“navLinkActive”);
}否则{
$(this.removeClass(“navLinkActive”);
}
});
});
});
//平滑滚动
jQuery(($)=>{
//滚动的速度(以毫秒为单位)
恒速=1000;
$('a[href*=“#”]”)
.filter((i,a)=>a.getAttribute(“href”).startsWith(“#”)a.href.startsWith(`${location.href}#`))
.unbind(“单击.smoothScroll”)
.bind(“单击.smoothScroll”,(事件)=>{
const targetId=event.currentTarget.getAttribute(“href”).split(“#”)[1];
const targetElement=document.getElementById(targetId);
if(targetElement){
event.preventDefault();
$(“html,body”).animate(
{
scrollTop:$(targetElement).offset().top,
},
速度
);
}
});
});
正文{
最小高度:300vh;
}
.粘的{
位置:固定;
排名:0;
左:0;
宽度:100%;
高度:60px;
z指数:10;
}
.导航链接{
颜色:#000;
}
.navLinkActive{
颜色:#ff0000;
}
.科{
高度:50vh;
}
#非导航部分{
最小高度:计算(100vh-60px);
}

与以下章节无关的内容

知识是一种美德,是一种美德,是一种美德,是一种美德

坐在阿梅特·康塞特图尔·阿利奎姆面前,告别精英


Leo a diam sollicitudin temporal id eu nisl.

@John Wu噢,天哪,我不敢相信我之前试过的语法不正确。非常感谢你!