Created
March 2, 2012 09:22
-
-
Save ishiduca/1957132 to your computer and use it in GitHub Desktop.
revolver.another.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function revolverSetUp (config) { | |
var focus = config.focus || 0, | |
list = tags(config.doms.list, 'li'), | |
last = list.length - 1, | |
lock = false, | |
pos = function (n) { | |
if (! isNumber(n)) n = 0; | |
config.doms.list.style.top = (config.doms.defaultTop - n) + "px"; | |
}, | |
reWidth = function (domids) { | |
var _width = (getBrowserWidth() - (config.doms.marginWidth * 2)) + "px"; | |
foreach(domids, function (domid) { | |
config.doms[domid].style.width = _width; | |
}); | |
}; | |
addEvent(window, 'resize', function () { reWidth(qw('list focus')); }); | |
pos(config.scroll.size * focus); | |
reWidth(qw('list focus')); | |
return { | |
roll : function (getNextFocus) { | |
if (! lock) { | |
var next = ((! isUndefined(getNextFocus)) && typeof getNextFocus === 'function') ? getNextFocus(focus) : focus; | |
if (next === 'LAST' || next > last) next = last; | |
if (next < 0) next = 0; | |
if (next !== focus) { | |
var isUp = (focus > next) ? true : false, | |
start = focus * config.scroll.size, | |
finish = next * config.scroll.size; | |
lock = setInterval(function () { | |
if ((isUp) ? (start < finish) : (start > finish)) { | |
clearInterval(lock); | |
lock = false; | |
pos(finish); | |
return; | |
} | |
start = (isUp) ? (start - config.scroll.pitch) : (start + config.scroll.pitch); | |
pos(start); | |
}, config.scroll.interval); | |
focus = next; | |
} | |
} | |
return this; | |
}, | |
fire : function (func) { | |
var link = tags(config.doms.list, 'a')[focus]; | |
if ((! isUndefined(func)) && typeof func === 'function') { | |
func(link, focus); | |
} else { | |
document.location = link.href; | |
} | |
return this; | |
} | |
}; | |
} | |
addEvent(window, 'load', function () { | |
var config, revolver, kb; | |
config = { | |
focus : 3, | |
doms : { | |
list : gid('main_ul'), | |
focus : gid('focus'), | |
defaultTop : (138 + 6), | |
marginWidth : (24 + 6) | |
}, | |
scroll : { | |
size : 36, | |
pitch : 6, | |
interval : 20 | |
} | |
}; | |
revolver = revolverSetUp(config); | |
kb = new Hotkey; | |
var _inc, _dec, _inc3, _dec3, _toTop, _toLast, _revolverFire, _help; | |
_inc = function () { | |
revolver.roll(function (now) { return now + 1; }); | |
}; | |
_dec = function () { | |
revolver.roll(function (now) { return now - 1; }); | |
}; | |
_inc3 = function () { | |
revolver.roll(function (now) { return now + 3; }); | |
}; | |
_dec3 = function () { | |
revolver.roll(function (now) { return now - 3; }); | |
}; | |
_toTop = function () { | |
revolver.roll(function (now) { return 0; }); | |
}; | |
_toLast = function () { | |
revolver.roll(function (now) { return 'LAST'; }); | |
}; | |
_revolverFire = function () { | |
revolver.fire(function (link, now) { | |
link.style.color = "#ff9933"; | |
setTimeout(function () { document.location = link.href; }, 250); | |
}); | |
}; | |
_help = function () { | |
var _off = false; | |
return function () { | |
_off = ! _off; | |
gid('navi').style.display = (_off) ? 'none' : 'block'; | |
}; | |
}(); | |
kb.add('j', _inc ).add('k', _dec) | |
.add('h', _inc3 ).add('l', _dec3) | |
.add('g', _toTop).add('G', _toLast) | |
.add(qw('space enter'), _revolverFire) | |
.add('q', _help) | |
; | |
}, false); | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment