How do I make the current/active thumbnail stay at 100% opacity in a Webflow image slider using SwiperJs?

Published on
September 22, 2023

To make the current or active thumbnail stay at 100% opacity in a Webflow image slider using SwiperJs, you can follow these steps:

  1. Add a custom code component: First, add a custom code component to your Webflow project. You can do this by dragging and dropping a "Custom Code" element from the "Add" panel onto your slide or thumbnail container.

  2. Link SwiperJs: Next, you will need to link SwiperJs to your project. To do this, copy the CDN link for the SwiperJs library from its official website (https://swiperjs.com/get-started/) and paste it into the "Head Code" section of your project settings. This will enable the necessary JavaScript and CSS files.

  3. Add custom JavaScript: Inside the custom code component that you added earlier, you can now insert the JavaScript code that will handle the opacity of the active thumbnail. Use the following code and ensure you customize it based on your CSS class names and structure:

<script>  // Wait for the document to load  document.addEventListener('DOMContentLoaded', function() {    // Initialize Swiper    var mySwiper = new Swiper('.swiper-container', {      // Set your desired settings for the Swiper instance      // ...            on: {        slideChange: function() {          // Reset opacity for all thumbnails          var thumbnailItems = document.querySelectorAll('.thumbnail-item');          thumbnailItems.forEach(function(item) {            item.style.opacity = '';          });                    // Set 100% opacity for active thumbnail          var currentSlide = mySwiper.realIndex;          thumbnailItems[currentSlide].style.opacity = '1';        },      },    });  });</script>
  • Replace '.swiper-container' with the class or ID of your Swiper container element.
  • Replace '.thumbnail-item' with the class or selector of your thumbnail elements.
  1. Style the thumbnails: Finally, apply CSS styles to the thumbnail elements to control their opacity. By default, all thumbnails should have an opacity less than 100%. This way, when a slide changes, the JavaScript code will reset the opacity for all thumbnails and apply 100% opacity to the active one.
/* CSS for the thumbnail items */.thumbnail-item {  /* Set the opacity less than 100% by default */  opacity: 0.7;}

Remember to customize the CSS class '.thumbnail-item' to match your thumbnail elements' class or selector.

This implementation ensures that the active thumbnail remains at 100% opacity while others become less opaque.

Additional Questions:

  1. How do I install SwiperJs in Webflow?
  2. Can I customize the transition effects in a Webflow image slider using SwiperJs?
  3. How can I add navigation buttons to a Webflow image slider using SwiperJs?