Skip to content

Instantly share code, notes, and snippets.

@radioman-00
Forked from NoUsername/imageViewer.html
Last active July 10, 2021 02:01
Show Gist options
  • Select an option

  • Save radioman-00/ae7bf170434200e368b11f75285fa49a to your computer and use it in GitHub Desktop.

Select an option

Save radioman-00/ae7bf170434200e368b11f75285fa49a to your computer and use it in GitHub Desktop.
Simple image viewer, which loads all images from an html directory listing.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<img id="viewer" src="" width="800px"/>
<div id="info"> </div>
<script>
$(function() {
var baseUrl = "/images/";
var pictureIndex = 0;
var pictures = [];
function getFiles() {
$.ajax(baseUrl).success(function(data) {
pictures = [];
var lastPicture = "";
$(data).find("a[href]").each(function() {
var href = $(this).attr('href');
if (href.indexOf('.jpg') > 0 || href.indexOf('.png') > 0 || href.indexOf('.jpeg') > 0) {
if (href != lastPicture) {
pictures.push(href);
lastPicture = href;
}
}
});
console.log(pictures.length + " pictures loaded!");
changePicture(0);
});
}
function changePicture(indexOffset) {
pictureIndex += indexOffset;
if (pictureIndex >= pictures.length) {
pictureIndex = 0;
} else if (pictureIndex < 0) {
pictureIndex = pictures.length - 1;
}
$('#viewer').attr('src', baseUrl + pictures[pictureIndex]);
$('#info').text((pictureIndex + 1) + "/" + pictures.length);
}
getFiles();
$(document).keydown(function(e){
var left = -1, right = 1;
if (e.keyCode == 37) {
changePicture(left); return false;
} else if (e.keyCode == 39) {
changePicture(right); return false;
}
});
});
</script>
</body>
</html>
@ScottyBeach
Copy link

Hi @https://gist.github.com/radioman-00-00. I get nothing at all. Not doubles, not singles. I'm with @https://gist.github.com/ThinkDigitalSoftware: how do we use this? I've tried putting this HTML file in the same directory level as another directory named "images" and putting some .jpgs in there. Not enough though, nothing happening. Hints? Thanks!@

  • Scott

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment