简体   繁体   English

通过向上和向下箭头导航到特定的锚定标签

[英]Navigate to specific anochor tag via up and down arrow press

I am currently using anchor tags and have implemented smooth scrolling on 5 links. 我目前正在使用定位标记,并已在5个链接上实现了平滑滚动。 This currently works perfectly. 目前,这可以完美运行。 However, I would now like to add the ability to use the arrow keys to navigate through these same anchor tags. 但是,我现在想添加使用箭头键浏览这些相同锚标记的功能。

I can only fumble through javascript and jquery, so I'm pretty confused when it comes to that stuff. 我只能通过javascript和jquery摸索,所以当涉及到这些东西时我很困惑。

<ul>
    <li class="scrolldot"><span><a href="#one">1</a></span></li>
    <li class="scrolldot"><span><a href="#two">2</a></span></li>
    <li class="scrolldot"><span><a href="#three">3</a></span></li>
    <li class="scrolldot"><span><a href="#four">4</a></span></li>
    <li class="scrolldot"><span><a href="#five">5</a></span></li>
    <li class="scrolldot"><span><a href="#six">6</a></span></li>
</ul>

So basically, I want a user to hit the down arrow and them to be taken to the next section, depending on where they are on the page. 因此,基本上,我希望用户单击向下箭头,然后将其转到下一部分,具体取决于它们在页面上的位置。 If they are on section 2, take them to three. 如果它们在第2节中,则将它们取三。 If they hit up again, they would be taken back to two and so forth. 如果他们再次击中,他们将被带回到两个,依此类推。 Make sense? 说得通?

Checkout Mousetrap . 结帐捕鼠器 It's a nifty little library that makes binding keys pretty easy. 这是一个漂亮的小库,使绑定键非常容易。 There are a few good examples on the site as well. 网站上也有一些很好的例子。

Perhaps something like this would suffice: 也许这样的事情就足够了:

Mousetrap.bind('up', function() {
    your_up_function();
});

Mousetrap.bind('down', function() {
    your_down_function();
});

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

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