this selected-block css styling working but when my button is active this selected-block div inside my button dont change color.
I don’t know it’s something wrong in css code (this codes are tested in codpen.io) and working perfectly but in Webflow published site just active button style don’t.
If anyone had anyone same problem and sucesfully find solution I would be more then happy to hear it.
Best
Oh! I forget mention code is embed element should I use it in head section?
You’ll need to share a link to the published page you’re trying to make this work on, and details ( maybe a screenshot ) of exactly which elements you’re trying to affect.
In general, chrome devtools is what you’ll want to use to identify where your CSS is missing the mark.
Oh! Hello Stan again thank you for all your assistance
Yes it works thank you but have one problem. Default starting position (when page is loaded) I guess does not count as active yet …so until go to next slide color is not changed then becomes active and change colors all the time on correct way and it works perfectly.
I just put in javascript change to force color change of active and it works as was inteded to do…also on default position when is loaded to screen
Placed as Embed Code in that div object.
<style>
.slides {
display: flex;
transition: transform 1s ease;
}
.nav-buttons {
background-color: transparent;
}
.nav-buttons button {
background-color: transparent;
cursor: pointer;
}
.nav-buttons button.active {
background-color: transparent;
cursor: pointer;
}
.selected-block {
background-color: #7b7b7b;
transition: background-color 0.3s ease;
}
</style>
<script>
const slides = document.querySelector('.slides');
const navButtons = document.querySelectorAll('.nav-btn');
const selectedBlocks = document.querySelectorAll('.selected-block');
let currentSlide = 0;
let autoplayInterval;
// Function to move to a specific slide
function goToSlide(slideIndex) {
slides.style.transform = `translateX(-${slideIndex * 1271}px)`;
currentSlide = slideIndex;
updateNavButtons();
resetAutoplay();
updateSelectedBlockColor(); // Call the function to update selected block color
}
// Function to update the active state of navigation buttons
function updateNavButtons() {
navButtons.forEach((button, index) => {
if (index === currentSlide) {
button.classList.add('active');
} else {
button.classList.remove('active');
}
});
}
// Function to update the background color of selected block
function updateSelectedBlockColor() {
selectedBlocks.forEach((block, index) => {
if (index === currentSlide) {
block.style.backgroundColor = '#14aed2'; // Change to desired color
} else {
block.style.backgroundColor = '#7b7b7b'; // Default color
}
});
}
// Event listeners for navigation buttons
navButtons.forEach((button, index) => {
button.addEventListener('click', () => {
goToSlide(index);
});
});
// Call the function to set default active state on page load
updateSelectedBlockColor();
// Function to reset autoplay timer
function resetAutoplay() {
clearInterval(autoplayInterval);
autoplayInterval = setInterval(() => {
currentSlide = (currentSlide + 1) % 3;
goToSlide(currentSlide);
}, 4000);
}
// Initial autoplay
resetAutoplay();
</script>
You can also make a minimal change to modulo use navButtons.length to be code more dynamic. But in this case as there will be only 3 slides is not important.