Clickable image slideshow | XM Community
Solved

Clickable image slideshow

  • 27 September 2021
  • 8 replies
  • 569 views

Hello,
I'm hoping someone can help me diagnose a problem with custom JS. Following these steps (https://www.w3schools.com/howto/howto_js_slideshow_gallery.asp), I can create an image gallery but the next/previous buttons are not functional.
A similar dead thread from 2018 (https://community.qualtrics.com/XMcommunity/discussion/2514/is-there-a-way-to-include-an-image-slideshow-in-my-survey) ran into this issue, but no solution was offered (the 2020 suggestion to use an Imgur album will not work, unless there is a way to prevent the imgur image from linking out of the survey and over to imgur.com).
Thank you!

icon

Best answer by qualtrics_nerd 27 April 2023, 01:17

View original

8 replies

Badge +8

Hi, Try using following HTML from the file and JS code from below, I haven't tested it myself but it should work. Please let me know if it doesn't.

HTML code file
html.txt
JS Code:
Qualtrics.SurveyEngine.addOnReady(function () {
    var $this = jQuery(this.questionContainer);
    
    var slideIndex = 1;
    
    function showSlides(n) {
        var i;
        var slides = document.getElementsByClassName("mySlides");
        var dots = document.getElementsByClassName("dot");
        if (n > slides.length) {
            slideIndex = 1;
        }
        if (n < 1) {
            slideIndex = slides.length;
        }
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";
        }
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active", "");
        }
        slides[slideIndex - 1].style.display = "block";
        dots[slideIndex - 1].className += " active";
    } 
    showSlides(slideIndex);

    // Next/previous controls
    jQuery("[data-plus]", $this).on("click", function() {
        var n = +jQuery(this).attr("data-plus");
        showSlides(slideIndex += n);
    });

    // Thumbnail image controls
    jQuery("[data-current]", $this).on("click", function() {
        var n = +jQuery(this).attr("data-current");
        showSlides(slideIndex = n);
    });
});

Aah, unfortunately it still doesn't allow the slides to advance! But I really want to thank you for trying to help me!

If it's relevant, this is what the source code looks like when I am on the page with that question:
source.txt

Badge +8

BetzD This will work and I have tested it 🙂
Just update the JS code with this one.
Qualtrics.SurveyEngine.addOnReady(function () {
    var $this = jQuery(this.questionContainer);

    var slideIndex = 1;

    function showSlides(n) {
        var i;
        var slides = document.getElementsByClassName("mySlides");
        var dots = document.getElementsByClassName("demo");
        var captionText = document.getElementById("caption");
        if (n > slides.length) {
            slideIndex = 1
        }
        if (n < 1) {
            slideIndex = slides.length
        }
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";
        }
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active", "");
        }
        slides[slideIndex - 1].style.display = "block";
        dots[slideIndex - 1].className += " active";
        captionText.innerHTML = dots[slideIndex - 1].alt;
    }
    showSlides(slideIndex);

    // Next/previous controls
    jQuery("[data-plus]", $this).on("click", function () {
        var n = +jQuery(this).attr("data-plus");
        showSlides(slideIndex += n);
    });

    // Thumbnail image controls
    jQuery("[data-current]", $this).on("click", function () {
        var n = +jQuery(this).attr("data-current");
        showSlides(slideIndex = n);
    });
});

Yes! This worked, and I am so appreciative of the effort you put into helping me! Hopefully someone with a similar issue will be able to benefit from this!

Badge

Hello- is it possible anyone is able to share the HTML code with me? The link appears to be broken. 

Thanks!

Userlevel 5
Badge +19

Hi @zreames ,

I don't have the above HTML file.


But you can implement the Slideshow  using below HTML code :

<style>* {
box-sizing: border-box;
}

/* Position the image container (needed to position the left and right arrows) */
.container {
position: relative;
}

/* Hide the images by default */
.mySlides {
display: none;
}

/* Add a pointer when hovering over the thumbnail images */
.cursor {
cursor: pointer;
}

/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 40%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-user-select: none;
}

/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}

/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}

/* Container for image text */
.caption-container {
text-align: center;
background-color: #222;
padding: 2px 16px;
color: white;
}

.row:after {
content: "";
display: table;
clear: both;
}

/* Six columns side by side */
.column {
float: left;
width: 16.66%;
}

/* Add a transparency effect for thumnbail images */
.demo {
opacity: 0.6;
}

.active,
.demo:hover {
opacity: 1;
}</style>



<!-- Container for the image gallery -->
<div class="container">

<!-- Full-width images with number text -->
<div class="mySlides">
<div class="numbertext">1 / 6</div>
<img src="https://www.w3schools.com/howto/img_woods_wide.jpg" style="width:100%">
</div>

<div class="mySlides">
<div class="numbertext">2 / 6</div>
<img src="https://www.w3schools.com/howto/img_5terre_wide.jpg" style="width:100%">
</div>

<div class="mySlides">
<div class="numbertext">3 / 6</div>
<img src="https://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
</div>

<div class="mySlides">
<div class="numbertext">4 / 6</div>
<img src="https://www.w3schools.com/howto/img_lights_wide.jpg" style="width:100%">
</div>

<div class="mySlides">
<div class="numbertext">5 / 6</div>
<img src="https://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
</div>

<div class="mySlides">
<div class="numbertext">6 / 6</div>
<img src="https://www.w3schools.com/howto/img_snow_wide.jpg" style="width:100%">
</div>

<!-- Next and previous buttons -->
<a class="prev" >&#10094;</a>
<a class="next" >&#10095;</a>

<!-- Image text -->
<div class="caption-container">
<p id="caption"></p>
</div>

<!-- Thumbnail images -->
<div class="row">
<div class="column">
<img class="demo cursor" src="https://www.w3schools.com/howto/img_woods_wide.jpg" style="width:100%" alt="The Woods">
</div>
<div class="column">
<img class="demo cursor" src="https://www.w3schools.com/howto/img_5terre_wide.jpg" style="width:100%" alt="Cinque Terre">
</div>
<div class="column">
<img class="demo cursor" src="https://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%" alt="Mountains and fjords">
</div>
<div class="column">
<img class="demo cursor" src="https://www.w3schools.com/howto/img_lights_wide.jpg" style="width:100%" alt="Northern Lights">
</div>
<div class="column">
<img class="demo cursor" src="https://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%" alt="Nature and sunrise">
</div>
<div class="column">
<img class="demo cursor" src="https://www.w3schools.com/howto/img_snow_wide.jpg" style="width:100%" alt="Snowy Mountains">
</div>
</div>
</div>



Just copy those in HTML(<>) tabs and replace them with link of your image files.

and add below JS to your question:

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
// Get the elements
var prevBtn = document.querySelector(".prev");
var nextBtn = document.querySelector(".next");
var slides = document.querySelectorAll(".mySlides");
var dots = document.querySelectorAll(".demo");

// Set the initial slide index
var slideIndex = 1;
showSlides(slideIndex);

// Function to move to the next slide
function plusSlides(n) {
showSlides(slideIndex += n);
}

// Function to show a specific slide
function currentSlide(n) {
showSlides(slideIndex = n);
}

// Function to display the slides
function showSlides(n) {
// Reset the index if it exceeds the number of slides
if (n > slides.length) {
slideIndex = 1;
} else if (n < 1) {
slideIndex = slides.length;
}

// Hide all the slides
for (var i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}

// Remove the active class from all the dots
for (var i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}

// Display the current slide and add the active class to its corresponding dot
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}

// Event listener for the previous and next buttons
prevBtn.addEventListener("click", function() {
plusSlides(-1);
});

nextBtn.addEventListener("click", function() {
plusSlides(1);
});

// Event listener for the dots
for (var i = 0; i < dots.length; i++) {
dots[i].addEventListener("click", function() {
var slideNumber = parseInt(this.alt.split(" ")[0]);
currentSlide(slideNumber);
});
}


});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});


Hope this resolves your query😊

Badge

This is great- thank you for the quick response!

I can’t say I’m code smart, so I have to ask- is there a way to hide the thumbnails? I noticed that I get an error when clicking on the thumbnail vs using the left right controls. 

I remove bits of code at a time, but the closest I’ve gotten is the first image appears with the left right arrows, but then doesn’t change accordingly (or at all). 

 

Edit:

Changed the thumbnail image width to 0% and got my results!

Leave a Reply