Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created January 12, 2009 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edvakf/45842 to your computer and use it in GitHub Desktop.
Save edvakf/45842 to your computer and use it in GitHub Desktop.
javascript: (function(doc) {
var add = doc.addEventListener;
add('mouseover',overHandler,false);
add('mouseout',outHandler,false);
add('click',clickHandler,false);
add('keypress',keyHandler,false);
function removeListeners(){
var rm = doc.removeEventListener;
rm('mouseover',overHandler,false);
rm('mouseout',outHandler,false);
rm('click',clickHandler,false);
rm('keypress',keyHandler,false);
}
var theElm;
function overHandler(e){
theElm = e.target;
addBG(theElm);
}
function outHandler(e){
removeBG(theElm);
}
function clickHandler(){
removeBG(theElm);
killOthers(theElm);
removeListeners();
}
function addBG(elm){
var st = elm.style;
if(st.backgroundColor)st.origBG = st.backgroundColor;
st.backgroundColor = 'orange';
}
function removeBG(elm){
if(!elm)return;
var st = elm.style;
st.backgroundColor = st.origBG||'';
st.origBG=null;
}
function keyHandler(e){
try{Hash[e.keyCode||e.which](e)&&e.preventDefault();}catch(e){}
}
var Hash = {
27:function(){removeBG(theElm);removeListeners();return true}/*Esc*/
,38:function(){if(theElm!=doc.body){removeBG(theElm);theElm=theElm.parentNode;addBG(theElm);}return true}/*up*/
,13:function(){clickHandler();return true;}/*Enter*/
};
function killOthers(ele){
while(ele!=doc.body){
[].forEach.call(ele.parentNode.childNodes,function(e){if(e!=ele && e.style)e.style.display='none'});
var st = ele.style;
st.width='100%';
st.padding=st.margin=0;
ele=ele.parentNode;
}
}
})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment