Skip to content

Instantly share code, notes, and snippets.

@AndreLion
Last active January 15, 2020 15:50
Show Gist options
  • Save AndreLion/c403453bc0afdab466fa to your computer and use it in GitHub Desktop.
Save AndreLion/c403453bc0afdab466fa to your computer and use it in GitHub Desktop.
轻单JK快捷键
(function(){
var style = document.createElement('style');
style.innerHTML = '.container ol.items li .title[shortcut] { border-color:#66afe9;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6); }';
document.head.appendChild(style);
var length = document.querySelector('ol.items').children.length;
var index = 0;
var inc = function(){
return index >= length ? index = length : index++;
}
var dec = function(){
return index <= 0 ? index = 0 : index--;
}
var mark = function(bool){
document.querySelector('ol.items li:nth-child('+(index)+') .title')[(bool?'set':'remove')+'Attribute']('shortcut','');
};
var nav = function(direction){
(index>0 && index<=length) && mark(false);
direction=='next'?inc():dec();
if(index !== 0){
mark(true);
scroll();
}else{
scroll(1);
}
};
var scroll = function(top){
var top = top || document.querySelector('ol.items li:nth-child('+index+') .title').getBoundingClientRect().top - document.body.getBoundingClientRect().top - document.querySelector('#sectionHeader').getBoundingClientRect().height - 15;
window.scrollTo(0,top);
};
var onKeydown = function(ev){
switch(ev.keyCode){
case 74: // j
nav('next');
break;
case 75: // k
nav('prev');
break;
default:
break;
}
}
document.addEventListener('keydown',onKeydown,false);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment