Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions css/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
body {
background-color: rosybrown;


}

#slideshow {
display:flex;
flex-wrap:wrap;
border: black solid;
height: 500px;
width: 500px;


}

#search {
padding: 10px 10px 10px 10px;
}

img {
max-width: 100%;
max-height: 100%;
}
22 changes: 18 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello Front-End</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/index.css" rel="stylesheet" />
<script src="js/index.js" defer></script>
<title>Slideshow</title>
</head>
<body>
<h1>Hello Front-End</h1>
<h1>Reddit Slideshow</h1>
<p>Enter 1 word search to see pictures!</p>
<form id="search-form">
<input type="text" min="1" id="user-search">
<input type="submit">
<input type="button" value="Stop" id="stop-show">
</form>
<div id="slideshow" class="Slideshow" data-ride="carousel">

</div>
</body>

</html>
59 changes: 59 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const form = document.getElementById('search-form')
const slideShow = document.getElementById('slideshow')
const stopButton = document.getElementById('stop-show')

let imageArray = []
let imageCount = 0

let startSlideShow

const showInitialImage = (responseArray) => {
let array = responseArray.data.children.map( data => data.data)
imageArray = array

while(slideShow.firstChild) {
slideShow.removeChild(slideShow.firstChild)
}
if (imageCount != 0) {
imageCount = 0
}
const picBox = document.createElement('img')
picBox.setAttribute('src', imageArray[imageCount].url)
slideShow.appendChild(picBox)
imageCount++
startSlideshow = setInterval(showNextImage, 2000)
}


const showNextImage = () => {
while(slideShow.firstChild) {
slideShow.removeChild(slideShow.firstChild)
}

if (imageCount < imageArray.length) {
const picBox = document.createElement('img')
picBox.setAttribute('src', imageArray[imageCount].url)
slideShow.appendChild(picBox)
imageCount++
}
}

const stopSlideShow = () => {
imageArray = []
clearInterval(startSlideShow)
}

stopButton.addEventListener('click', stopSlideShow);

form.addEventListener('submit', event => {

let userInput = document.getElementById('user-search').value

event.preventDefault()
fetch(`https://www.reddit.com/search.json?q=${userInput}+nsfw:no+type:image`)
.then(res => res.json())
.then(showInitialImage)
.catch(console.error)
})