Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Carousel slider with one image

KapioKapio Member ✭✭

What is the best carousel page, where i can have x number of slides, and put only 1 image, and slide through it?


Best Answer


  • Moses_SundayMoses_Sunday Member ✭✭

    <div class="slideshow-container"><div class="mySlides fade"><img src='./assets/img/web-crab-compressed.jpg' style='width: 100%;' alt="sally lightfoot crab"/> <div class="text">Sally Lightfoot Crab</div> </div><div class="mySlides fade"><img src='picture.jpg' style='width: 100%;' alt="fighting nazca boobies"/> <div class="text">A scuffle between two Nazca Boobies</div> </div><div class="mySlides fade"><img src='another_picture.jpg' style='width: 100%;' alt="otovalo waterfall"/> <div class="text">A waterfall discovered on a hike in Otovalo</div> </div><div class="mySlides fade"><img src='insert_picture.jpg' style='width: 100%;' alt="pelican"/> <div class="text">Pelican</div> </div><a class="prev" onclick='plusSlides(-1)'>&#10094;</a> <a class="next" onclick='plusSlides(1)'>&#10095;</a> </div> <br/>

    The function I will introduce first is plusSlides.

    function plusSlides(n){ clearInterval(myTimer); if (n < 0){ showSlides(slideIndex -= 1); } else { showSlides(slideIndex += 1); } if (n === -1){ myTimer = setInterval(function(){plusSlides(n + 2)}, 4000); } else { myTimer = setInterval(function(){plusSlides(n + 1)}, 4000); } }

    I’ll explain the initialization and clearInterval in a minute. But setInterval is the function I used because it essentially setTimeouts something at a repeated interval. You can setTimeout to execute the plus slides function, but then you’d have to repeat that function again. To avoid unnecessary redundancy, setInterval is just what you need. It sets the interval in which you determine a function to execute, and when/how often you want to execute it.

    Essentially, this function initializes the slideshow as well as decide whether to go forward or backward within the sequence. And what determines the forward/backward functionality? The n that is passed into the function. If n is negative, the slideshow index selected will be the index prior to the current one. So if you are on the fourth picture (at index three), and you click the back arrow, you are selecting the slide at index as the current slide to be shown. If n is positive, you would go to the next index in the sequence. So if you’re on the slide at index two and you click next, you will bring up the next slide, at index three. This corresponds to another function I’ve written, called showSlides.

    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"; }

    The function gathers the slide elements and the dots elements. These will be stored in arrays. The items in these arrays will be selected based on their relation to slideIndex. showSlides determines which slide to show and which slides to hide when you select next or previous, or if you let the slideshow run automatically. As you can see, there is on setInterval or clearInterval here. This function serves only to apply the appropriate CSS classes and styles given the current slide index. There were other code snippets online that did the same thing as showSlides, but the changing of classes was over complicated. I simplified class assignment process for the dots (which actually has significant style changes, like color and opacity), and simply changed the display to either block or none for the slides. I incorporated a loop to remove styles/classes from all slides/dots that are not found at the current index. Since the number of dots will correspond to the number of pictures, the slideIndex can be applied as the current index for slides as well as the dots.

    Now it’s time to set everything in motion.

    window.addEventListener("load",function() { showSlides(slideIndex); myTimer = setInterval(function(){plusSlides(1)}, 4000); })

    We initialize both myTimer and slideIndex. slideIndex is set to one (throughout the function we refer to the indexes as slideIndex-1). myTimer will be assigned the interval that we will set for the slideshow. When the page loads, it first runs the showSlides(n) function, to determine which slide to show first. Then it reassigns myTimer a function and interval in which that function (plusSlides) should execute at a specific time (every 4,000 milliseconds).

    But what about the dots? Are they simply an aesthetic choice?


    They also serve a functional purpose. Let's look at the HTML.

    <div style='text-align: center;'> <span class="dot" onclick='currentSlide(1)'></span> <span class="dot" onclick='currentSlide(2)'></span> <span class="dot" onclick='currentSlide(3)'></span> <span class="dot" onclick='currentSlide(4)'></span> </div>

    As you can see, each of the dots has an onclick function, called currentSlide. currentSlide takes an argument of number. What will this number represent?

    You got it, slideIndex. Below is the currentSlide function.

    function currentSlide(n){ clearInterval(myTimer); myTimer = setInterval(function(){plusSlides(n + 1)}, 4000); showSlides(slideIndex = n); }

  • KapioKapio Member ✭✭

    Hmm, I'm talking about Xamarin Forms, mobile app

Sign In or Register to comment.