JS / Jquery代码打算在菜单/导航上突出显示用户的当前位置 [英] JS/Jquery code which intends to highlight on the menu/navigation, the current location of the user

查看:72
本文介绍了JS / Jquery代码打算在菜单/导航上突出显示用户的当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript和Jquery的新手,我正在寻找一些关于以下代码无法正常运行的帮助/指导。该代码旨在突出显示菜单/导航,用户的当前位置。我的网站上有多个导航菜单,用于风格目的。每个主要的div都有一个随附的导航菜单。



这是我的代码,突出显示一个特定的导航:



New to JavaScript and Jquery, I'm looking for some help/guidance as to why the following code is not functioning properly. The code intends to highlight on the menu/navigation, the current location of the user. I have multiple navigation menus on my site for stylistic purposes. Each major div has an accompanying nav menu.

Here's my code to highlight one specific navigation:

$(window).scroll(function(){ // when the window/view port is scrolled
 var windowPos = $(window).scrollTop(); // get the offset of the window from the top of page
 var theID = $("#about-me"){ //div in question
 var divPos = theID.offset().top; // get the offset of the div from the top of page
 var divHeight = theID.height(); // get the height of the div in question
  if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
  $("#nav2 li:nth-child(2) a").addClass("active");
  } else {
  $("#nav2 li:nth-child(2) a").removeClass("active");
  }
 }
});





有人能指出我正确的方向吗?< br $> b $ b

非常感谢!



Can anyone point me in the right direction?

Many thanks!

推荐答案

window )。scroll( function (){ // when窗口/视图端口滚动
var windowPos =
(window).scroll(function(){ // when the window/view port is scrolled var windowPos =


window )。scrollTop(); // 从顶部获取窗口的偏移量of page
var theID =
(window).scrollTop(); // get the offset of the window from the top of page var theID =


#about-me){ // div有问题
var divPos = theID.offset()。top; // 从页面顶部获取div的偏移量
var divHeight = theID.height(); // 获取有问题的div的高度
if (windowPos> = divPos&& windowPos<(divPos + divHeight)){
("#about-me"){ //div in question var divPos = theID.offset().top; // get the offset of the div from the top of page var divHeight = theID.height(); // get the height of the div in question if (windowPos >= divPos && windowPos < (divPos + divHeight)) {


这篇关于JS / Jquery代码打算在菜单/导航上突出显示用户的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆