简体   繁体   English

如果当前存在,如何更改内部链接的样式

[英]How to change style of internal link if currently there

I'm trying to create a navigation menu for what I expect to be an extremely long page. 我正在尝试为我期望的非常长的页面创建导航菜单。 I'm using internal links to make it easy to get from one section or another, but the page is designed for users to scroll up and down the page quite a bit in one session. 我正在使用内部链接,以便从一个部分或另一个部分轻松获取,但该页面旨在让用户在一个会话中相当多地向上和向下滚动页面。 I found that internal links aren't enough to orient the user with their location on the page, so I've added smooth scrolling when an internal link is clicked. 我发现内部链接不足以定位用户在页面上的位置,因此我在单击内部链接时添加了平滑滚动。 This has helped some, but I would love to change the style of the internal link if you're currently next to it. 这对一些人有所帮助,但如果您现在正在旁边,我很乐意改变内部链接的风格。

As an example we have five links below. 作为一个例子,我们在下面有五个链接。 The user starts out at the section that corresponds with link A, so it's bold faced. 用户从与链接A对应的部分开始,因此它是大胆的。 When they scroll down a bit further they get to the section that corresponds with link B, so it is now bold faced. 当它们向下滚动一点时,它们会到达与链接B对应的部分,因此现在它是大胆的。 If they jump down to the section that corresponds with link E it becomes bold faced, and the same if they jump to link C. 如果他们跳到与链接E对应的部分,则变为粗体,如果他们跳转到链接C则相同。

~

Step 1 ............... Step 2 ............... Step 3 ............... Step 4 第1步 ............... 第2 ............... 第3步 .............. 第4步

Link A ...............Link A...............Link A...............Link A 链接A ...............链接A ...............链接A .............. .Link A.

Link B............... Link B ...............Link B...............Link B 链接B ............... 链接B ...............链接B .............. .Link B.

Link C...............Link C...............Link C............... Link C 链接C ...............链接C ...............链接C .............. 。 链接C.

Link D...............Link D...............Link D...............Link D 链接D ...............链接D ...............链接D .............. .Link D.

Link E...............Link E............... Link E ...............Link E 链接E ...............链接E ............... 链接E .............. .Link E.

~

Is there any way to achieve this? 有没有办法实现这个目标?

You could probably use "hover" event, assuming the user's pointer will get over the div where is your content linked whith A, B, C, ... 您可以使用“悬停”事件,假设用户的指针将越过div,您的内容链接在A,B,C,......

But it wouldn't work in all cases... you could also count how much the user has scrolled down, and then determine where he is in the page. 但它并不适用于所有情况......您还可以计算用户向下滚动的程度,然后确定他在页面中的位置。

That's not an easy matter. 这不是一件容易的事。

使用javascript跟踪每个部分的滚动顶部位置,如果用户输入该部分,请使用javascript查找与其关联的链接,并根据需要添加一个类,同时从上一个链接中删除该类。

One way would be is to use javascript to find all the internal links then get the offsetTop This returns the number of pixels from the top of the page to the element. 一种方法是使用javascript查找所有内部链接然后获取offsetTop这将返回从页面顶部到元素的像素数。 Then I'm not sure if there is a reliable way to tell if the page has been scrolled. 然后我不确定是否有一种可靠的方法来判断页面是否已滚动。 If not then poll every couple of seconds to see what it's position is and then compare to the offsetTop. 如果没有,则每隔几秒轮询以查看它的位置,然后与offsetTop进行比较。 There may be better solutions then this however. 然而,可能有更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM