Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
20
Help us understand the problem. What is going on with this article?
@syui

自身のホームページやWebサイトに独自のキーバインドを設定する方法

More than 5 years have passed since last update.

GoogleやHatenaのサイトでは、実はviのキーバインドが有効になっています。つまり、jkでコンテンツを選択できるようになっています。

今回は、このような機能を自身のホームページやWebサイトで設定してみようということで、この記事を書きます。

使用するのは、jeresig/jquery.hotkeysです。使い方は、以下の様なコードを書けばOKです。

js/hotkeys-vi.js
$(function(){
    $(document).bind('keydown', 'j', function(){      
        $(".move:focus").closest('li').next().find('a.move').focus();   
    })
});

$(function(){
    $(document).bind('keydown', 'k', function(){      
        $(".move:focus").closest('li').prev().find('a.move').focus();   
    })
});

$(function(){
    $(document).bind('keydown', 'g', function(){      
        document.location.href = '#';
    })
});

$(function(){
    $(document).bind('keydown', 'Shift+g', function(){      
        document.location.href = '#footer';
    })
});

$('.move').click(function() {
    this.focus();
});

$(function() {
    $('#first').focus();
});
head.html
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="https://raw.githubusercontent.com/jeresig/jquery.hotkeys/master/jquery.hotkeys.js"></script>
<script type="text/javascript" src="js/hotkeys-vi.js"></script>
index.html
<ul>
<li> <a id="first" href="#" class='move'>Link</a> </li>
<li><a href="#" class="move">Link</a></li>
<li><a href="#" class="move">Link</a></li>
<li><a href="#" class="move">Link</a></li>
</ul>

これで、jkでコンテンツのフォーカスを移動できるようになります。

20
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
syui

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
20
Help us understand the problem. What is going on with this article?