简体   繁体   English

如何使用键盘箭头键在HTML元素之间导航

[英]How to navigate between HTML element using keyboard arrow keys

I want my web page to allow navigation between blocks of text using the left and right arrow keys on a keyboard. 我希望我的网页允许使用键盘上的向左和向右箭头键在文本块之间导航。 I assume this would require Jquery. 我认为这将需要Jquery。

I have literally read a dozen posts on Stackoverflow that seemingly address this question without really answering it...so please don't close this as a duplicate question 我已经在Stackoverflow上阅读了十几个帖子,这些帖子似乎是在解决这个问题而没有真正回答...所以请不要将此重复作为一个重复的问题

UPDATE: I simply want to scroll between articles (like blog posts) using the arrow keys. 更新:我只是想使用箭头键在文章(如博客文章)之间滚动。 All I want is to scroll the page to the next divider. 我想要的只是将页面滚动到下一个分隔线。

Here's a JS Fiddle I made for you: http://jsfiddle.net/3NAwz/ 这是我为您制作的JS小提琴: http : //jsfiddle.net/3NAwz/

This code shows div tags that contain text, floating to the left within a wrapper. 这段代码显示了包含文本的div标签,该标签在包装器中浮动到左侧。 That is then contained in another wrapper that is smaller than the overall text (one section of it). 然后将其包含在另一个包装中,该包装小于整体文本(其中一部分)。

jQuery is then used to scroll along the content using the arrow keys. 然后使用jQuery使用箭头键在内容上滚动。 The .not(":animated") part stops it from animating if it is already animating. 如果.not(":animated")部分已在制作动画,则阻止它制作动画。 Using this just makes it nicer without a horrible long queue after some 5 year old decides to be very impatient. 在大约5岁的孩子决定非常耐心之后,使用它只会使它变得更好,而不会造成冗长的排队。

HTML 的HTML

<div class="wrapper">
    <div class="inner">
        <div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas aliquam erat vitae nibh convallis quis luctus turpis dignissim. Suspendisse imperdiet interdum consequat. Curabitur erat nisl, gravida et aliquet vitae, faucibus vitae lacus.</div>
        <div class="text">Donec dignissim lorem eu augue tincidunt facilisis. Etiam leo purus, tristique eu tempus gravida, auctor sit amet velit. Quisque mattis, libero ut pharetra luctus, tellus justo viverra elit, sodales scelerisque ipsum magna vel felis.</div>
        <div class="text">Pellentesque ligula mauris, volutpat id molestie posuere, fermentum eu lacus. Donec quis metus nisi, et accumsan massa. Morbi eget augue eget lorem semper cursus sit amet non tortor. Aliquam tempor rhoncus odio sit amet fermentum.</div>
    </div>
</div>​

CSS 的CSS

.wrapper {
    width:340px;
    overflow:hidden;
    border:1px #000 solid;
}
.inner {
    width:1020px;
}
.text {
    float:left;
    width:300px;
    padding:20px;
    font-size:14px;
    font-weight:bold;
    font-family:Arial;
}

jQuery jQuery的

$(document).ready(function() {
    $(document).keyup(function(event) {
        var key = event.which;
        if(key == 37) { // Left arrow key
            $(".wrapper").not(":animated").animate({ scrollLeft: "-=340px" }, 500);
        }
        if(key == 39) { //Right arrow key
            $(".wrapper").not(":animated").animate({ scrollLeft: "+=340px" }, 500);
        }
    });
});​

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

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