I'm diving into the world of JavaScript and decided to try my hand at creating an image slider. I managed to put together a basic version by following a couple of tutorials, and although it's working fine, I want to move it to an external js file (which I've already done) and incorporate a module pattern to utilize 'private' variables.
Can someone guide me on how to implement this in a module pattern? Below is what I currently have:
slider.js
(function() {
var images = ['img/1.png', 'img/2.png', 'img/3.jpg'];
var imgNum = 0;
var imgLength = images.length - 1;
function changeImage(direction) {
imgNum += direction;
if (imgNum > imgLength) {
imgNum = 0;
}
if (imgNum < 0) {
imgNum = imgLength;
}
document.getElementById('slideshow').src = images[imgNum];
return false;
}
window.setInterval(function() {
changeImage(1);
}, 30000);
return {
//Not sure what to put here
}
})();
index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test Page</title>
<link rel="stylesheet" type='text/css' href='style.css'>
<script src="slider.js"></script>
</head>
<body>
<img src="img/1.png" alt='football1' id='slideshow'>
<a href="#" onclick="return changeImage(-1)">Previous</a><br/>
<a href="#" onclick="return changeImage(1)">Next</a>
</body>
</html>