Skip to content

Instantly share code, notes, and snippets.

@rainyjune
Created June 10, 2013 12:53
Show Gist options
  • Save rainyjune/5748483 to your computer and use it in GitHub Desktop.
Save rainyjune/5748483 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Window onload event</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>TODO write content</div>
<img src="1.jpg" alt="1.jpg" width="200" height="200" id="pic"><br>
<script src="slide.js"></script>
</body>
</html>
var fn =(function () {
var imgs = ['1.jpg','2.jpg','3.jpg'];
var idx = 0;
var midx = imgs.length - 1;
return function(direction){
console.log('fire');
idx = idx + direction;
if (idx > midx) {
idx = 0;
}
if (idx < 0) {
idx = midx;
}
document.getElementById('pic').src = imgs[idx];
};
})();
document.onkeydown = keyHit;
function keyHit(evt) {
var lftArrow = 37;
var rgtArrow = 39;
if (evt) {
var thisKey = evt.which;
} else {
var thisKey = window.event.keyCode;
}
if (thisKey === lftArrow) {
console.log('left');
fn(-1);
} else if (thisKey === rgtArrow) {
console.log('right');
fn(1);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment