Update: Here is Take 2.
I want a slideshow with three primary pieces of functionality:
- Has text links that act as bullet-points and can jump directly to a slide when clicked.
- Pauses when a slide or link is moused over.
- Resumes resumes playing from the currently active slide if the user clicks a link.
This is what I came up with. Thanks for your feedback.
$(function() {
var slideCount = $("#carousel-slides > img").length
var slideToActivate = 1
var viewingLength = 2000
var loop
automateTheCarousel()
/* === events handlers === */
$("#carousel-nav").on("click", "a", function() {
slideToActivate = $(this).data("linked-image")
changeCarouselSlide()
})
$("#carousel-slides, #carousel-nav").hover(stopTheCarousel, automateTheCarousel)
/* === functions === */
function automateTheCarousel() {
loop = setInterval(goToNextCarouselSlide, viewingLength)
}
function stopTheCarousel() {
clearInterval(loop)
}
function goToNextCarouselSlide() {
slideToActivate++
if (slideToActivate > slideCount) {
slideToActivate = 1
}
changeCarouselSlide()
}
function changeCarouselSlide() {
// Deactivate everything.
$("#carousel-slides img").hide()
$("#carousel-nav a").css({
"font-weight": "normal"
})
// Activate
$("#" + slideToActivate).show()
$("a[data-linked-image=" + slideToActivate + "]").css({
"font-weight": "bold"
})
}
});
#carousel-slides {
float: left;
width: 50%;
}
#carousel-slides img {
height: 200px;
width: 200px;
display: block;
margin: 0 auto;
}
#carousel-nav {
float: right;
width: 50%;
text-align: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="carousel-slides">
<img id="1" src="https://i.sstatic.net/aKmK0.png">
<img id="2" style="display: none" src="https://i.sstatic.net/4V0IS.jpg">
<img id="3" style="display: none" src="https://i.sstatic.net/QtNO8.jpg">
</div>
<div id="carousel-nav">
These are the tests!!!
<br>
<a href="#" data-linked-image="1" style="font-weight: bold" }>Test 1</a>
<br>
<a href="#" data-linked-image="2">Test 2</a>
<br>
<a href="#" data-linked-image="3">Test 3</a>
<br>And here is some more text.
</div>
One small note: the inline styling is there to visually set the first slide as "active" when the page loads. I feel like there's a better way, though.