My slideshow script uses the onclick
event window.location.reload()
to advance to the next mini-slideshow, causing the page to flicker when the “NEXT Plant” button is clicked.
Ideally, the onclick
event should trigger a function to advance the slideshow, eliminating the need to reload the page.
Creating such a function, unfortunately, is easier said than done.
Intuitively, my first thought was to forego the onclick
event window.location.reload()
method and instead have the onclick
event call the onLoad
function runShow()
, thinking that re-invoking this script would advance the slideshow. It didn’t.
Re-invoking other functions also failed to advance the slideshow, and now I’m out of ideas what to try next.
Please advise. Thanks.
body {
display: flex;
justify-content: center;
text-align: center;
background-color: black;
}
img {
display: block;
position: relative;
top: 20px;
max-width:100%;
max-height:calc(100vh - 160px - ((.4em + .6vmin) + (.4em + .6vmax)));
object-fit: contain;
}
.caption {
position: absolute;
bottom: 120px;
font-size: calc((.4em + .6vmin) + (.4em + .6vmax));
color: white;
left: 0;
right: 0;
width: 100%;
text-align: center;
}
p {
position: absolute;
bottom: 10px;
left: 0;
right: 0;
width: 100%;
text-align: center;
}
.button {
border-radius: 8px;
background-color: green;
border: none;
color: black;
padding: 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
}
<!DOCTYPE html>
<html>
<head>
<title>Plant Slideshow</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
</head>
<body onLoad="runShow()">
<img id="slide" onmouseover="stopShow()" onmouseout="runShow()" src="" alt="">
<script>
var genusSpecies={"Adam's Needle (Yucca filamentosa)":["Adam's Needle (Yucca filamentosa)1.jpg"],"Virginia Wild Rye (Elymus virginicus)":["Virginia Wild Rye (Elymus virginicus)1.jpg","Virginia Wild Rye (Elymus virginicus)2.jpg"]};
if (window.innerHeight > 1000) {
var specificResolution="./plants1220/"; // Higher-resolution photos for desktops
} else {
var specificResolution="./plants500/"; // Lower-resolution photos for smartphones
}
var curimg=0;
var keys=Object.keys(genusSpecies); // Creates array of keys
var plantNumber=Object.keys(genusSpecies).length;
x=Math.floor(Math.random() * (plantNumber)); // Selects random index number for “keys” array, the element’s value providing a named key for “genusSpecies”
var plant=genusSpecies[keys[x]]; // Value of named key (image file names of specific mini-slideshow)
function swapImage()
{
document.getElementById("slide").setAttribute("src",specificResolution+plant[curimg])
curimg=(curimg<plant.length-1)? curimg+1 : 0; timer = setTimeout("swapImage()",4000);
}
function stopShow()
{
clearTimeout(timer);
}
function runShow()
{
swapImage();
}
</script>
<div class="caption">
<script>
document.write(keys[x]); // Displays caption
</script>
</div>
<p><button class="button" onclick="window.location.reload()">NEXT Plant<br>(hover over slideshow to pause)</button></p>
<!-- Reloads page, advancing the slideshow, but is inefficient & causes flickering -->
</body>
</html>