Skip to content

Commit

Permalink
Refactor code for showing random images
Browse files Browse the repository at this point in the history
Save code in new file, randomImg.js. Code also updates img alt
attribute. Issue #27
  • Loading branch information
CodeWritingCow committed Apr 12, 2018
1 parent d3ab55c commit 09540e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
16 changes: 16 additions & 0 deletions public/assets/javascript/randomImg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const imgCount = 6;
const dir = '/public/assets/img/';
const randomCount = Math.round(Math.random() * (imgCount - 1)) + 1;
const images = new Array;
images[1] = {"img": "michael-browning-14090-unsplash-1024x683.jpg", "alt": "Men working in kitchen. Photo by Michael Browning on Unsplash."};
images[2] = { "img": "michael-browning-14092-unsplash-1024x683.jpg", "alt": "Person, table, night and mercantile establishment. Photo by Michael Browning on Unsplash."};
images[3] = { "img": "joanna-boj-17158-unsplash-1024x683.jpg", "alt": "Cook in a cafe. Photo by Joanna Boj on Unsplash."};
images[4] = { "img": "making-waffles-cutting-them-into-pieces-picjumbo-com-1024x683.jpg", "alt": "Making waffles cutting them into pieces. Photo by picjumbo.com."};
images[5] = { "img": "clem-onojeghuo-97089-unsplash-1024x683.jpg", "alt": "Asian Street food: Letchworth Garden City, United Kingdom. Photo by Clem Onojeghuo on Unsplash."};
images[6] = { "img": "woman-holding-an-ice-cream-picjumbo-com-1024x683.jpg", "alt": "Woman holding an ice cream. Photo by picjumbo.com."};

const randomImg = dir + images[randomCount].img;
const randomImgAlt = images[randomCount].alt;

document.getElementById("parallaxImg").src = randomImg;
document.getElementById("parallaxImg").alt = randomImgAlt;
15 changes: 2 additions & 13 deletions views/home.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@
$(".button-collapse").sideNav();
$('.parallax').parallax();
});
const imgCount = 6;
const dir = '/public/assets/img/';
const randomCount = Math.round(Math.random() * (imgCount - 1)) + 1;
const images = new Array;
images[1] = "michael-browning-14090-unsplash-1024x683.jpg";
images[2] = "michael-browning-14092-unsplash-1024x683.jpg";
images[3] = "joanna-boj-17158-unsplash-1024x683.jpg";
images[4] = "making-waffles-cutting-them-into-pieces-picjumbo-com-1024x683.jpg";
images[5] = "clem-onojeghuo-97089-unsplash-1024x683.jpg";
images[6] = "woman-holding-an-ice-cream-picjumbo-com-1024x683.jpg";
const randomImg = dir + images[randomCount]
document.getElementById("parallaxImg").src=randomImg;
</script>
</script>
<script type="text/javascript" src="/public/assets/javascript/randomImg.js"></script>
</html>

0 comments on commit 09540e1

Please sign in to comment.