Dynamic Responsive Carousel with API Fetching and Touch Gestures

In HTML and jQuery, how would you create a responsive image carousel that dynamically fetches images from an external API, and allows users to navigate through the images using touch gestures on mobile devices? Additionally, the carousel should support lazy loading of images to optimize performance.

Asked by: Team_Jack

Answers:

I think this code will work.

HTML:

<div id="carousel">
  <ul id="image-list"></ul>
  <a href="#" id="prev-button">&lt;</a>
  <a href="#" id="next-button">&gt;</a>
</div>

CSS:

#carousel {
  position: relative;
  width: 100%;
  overflow: hidden;
}

#image-list {
  list-style-type: none;
  margin: 0;
  padding: 0;
  width: 100%;
  display: flex;
  transition: transform 0.3s ease;
}

#image-list li {
  flex: 0 0 100%;
}

#carousel img {
  display: block;
  width: 100%;
}

#prev-button,
#next-button {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  padding: 10px;
  color: #fff;
  background-color: rgba(0, 0, 0, 0.5);
  text-decoration: none;
}

#prev-button {
  left: 10px;
}

#next-button {
  right: 10px;
}

JavaScript (jQuery):

$(document).ready(function() {
  var apiUrl = 'https://example.com/api/images'; // Replace with your API endpoint

  // Fetch images from the API
  $.get(apiUrl, function(response) {
    var images = response.images;

    // Iterate through the images and append them to the carousel
    $.each(images, function(index, image) {
      var li = $('
  • ' + image.alt + '
  • '); $('#image-list').append(li); }); // Initialize the carousel $('#carousel').carousel(); }); // Carousel functionality $.fn.carousel = function() { var carousel = $(this); var imageList = carousel.find('#image-list'); var prevButton = carousel.find('#prev-button'); var nextButton = carousel.find('#next-button'); var currentImage = 0; var imageCount = imageList.find('li').length; var imageWidth = imageList.find('li').first().width(); // Update carousel dimensions on window resize $(window).on('resize', function() { imageWidth = imageList.find('li').first().width(); }); // Navigate to the previous image prevButton.on('click', function(e) { e.preventDefault(); currentImage = (currentImage - 1 + imageCount) % imageCount; moveCarousel(); }); // Navigate to the next image nextButton.on('click', function(e) { e.preventDefault(); currentImage = (currentImage + 1) % imageCount; moveCarousel(); }); // Move the carousel to the current image function moveCarousel() { var position = -currentImage * imageWidth; imageList.css('transform', 'translateX(' + position + 'px)'); } // Touch gesture support for mobile devices var touchStartX = 0; var touchEndX = 0; carousel.on('touchstart', function(e) { touchStartX = e.originalEvent.touches[0].clientX; }); carousel.on('touchend', function(e) { touchEndX = e.originalEvent.changedTouches[0].clientX; handleSwipe(); }); function handleSwipe() { if (touchEndX < touchStartX) { // Swiped left (next image) currentImage = (currentImage + 1) % imageCount; moveCarousel(); } else if (touchEndX > touchStartX) { // Swiped right (previous image) currentImage = (currentImage - 1 + imageCount) % imageCount; moveCarousel(); } } // Lazy loading of images var lazyImages = imageList.find('img[data-src]'); lazyImages.each(function() { var lazyImage = $(this); lazyImage.attr('src', lazyImage.attr('data-src')); lazyImage.removeAttr('data-src'); }); }; });

    Replace the "apiUrl" variable with the appropriate API endpoint that provides the images in the desired format.

    Answered by: Wouadud Al Reon

    Answer:

    Related Pages:

    • Creating a Clone of Themeforest

      How can I create a clone website similar to Themeforest using HTML, CSS, and JavaScript? Need guidance on the essential steps and techniques to achieve this.

    • Yelp Clone: HTML, Bootstrap, and JavaScript

      What are the essential steps and code snippets required to build a Yelp clone website using HTML, Bootstrap, and JavaScript? I'm looking for guidance on implementing key features and functionalities, such as user reviews, search functionality, and responsive design. Any insights and code examples would be greatly appreciated!

    • Items keep going outside the container

      Why do the items continuously overflow beyond the boundaries of the container despite attempts to contain them?

    • CSS Bottom Navbar

      How can I make a navbar at the bottom of a webpage using CSS?

    • Real-time Animal Sketch Classification using TensorFlow.js: Designing Interactive Website Components

      Suppose you want to build an interactive website where users can draw sketches of animals, and TensorFlow.js will classify the drawn sketches into different animal categories in real-time. How would you design the HTML, JavaScript, and TensorFlow.js components to achieve this functionality?