You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

轮播组件按钮下划线移除与Fade Out动画修改技术咨询

Hey Kenken! Let's tackle those two carousel problems you're facing. I'll walk you through each solution with clear code modifications you can drop right into your project.


1. Remove Underline on Hover for Next/Prev Buttons

The underline you see is the default browser styling for <a> tags—since your prev/next controls are anchor elements, we just need to explicitly turn off text decoration for them, including when hovering.

Update your CSS for the .prev and .next classes like this:

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  color: black;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  text-decoration: none; /* Add this line to remove default underline */
}

/* Optional: Ensure hover state doesn't re-add underline */
.prev:hover, .next:hover {
  text-decoration: none;
}

That will completely eliminate the underline from both buttons, no matter the state.


2. Switch from Fade-In to Fade-Out Animation

Right now, your carousel makes new slides fade in from transparent to visible. To switch to a fade-out effect (where the active slide fades away before the next one appears), we need to adjust both the CSS keyframes and the slide-switching logic.

Step 1: Add Fade-Out Animation to CSS

Replace your existing .fade animation with this fade-out variant:

/* Replace the original .fade class and keyframes */
.fade-out {
  -webkit-animation-name: fadeOut;
  -webkit-animation-duration: 1.5s;
  animation-name: fadeOut;
  animation-duration: 1.5s;
}

@-webkit-keyframes fadeOut {
  from { opacity: 1; }
  to { opacity: 0; }
}

@keyframes fadeOut {
  from { opacity: 1; }
  to { opacity: 0; }
}

/* Keep this if you want new slides to fade in, remove if you want instant appearance */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from { opacity: .1; }
  to { opacity: 1; }
}

@keyframes fade {
  from { opacity: .1; }
  to { opacity: 1; }
}

Step 2: Modify the JavaScript to Handle Fade-Out

We need to update the showSlides function to first fade out the current slide, then switch to the next one after the animation finishes. Here's the updated code:

var slideIndex = 1;
var timer = null;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");

showSlides(slideIndex);

function plusSlides(n) {
  clearTimeout(timer);
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  clearTimeout(timer);
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var currentActiveSlide = slides[slideIndex - 1];

  // Handle slide index wrapping
  if (n == undefined) {
    n = ++slideIndex;
  }
  if (n > slides.length) {
    slideIndex = 1;
  }
  if (n < 1) {
    slideIndex = slides.length;
  }

  if (currentActiveSlide) {
    // Apply fade-out animation to the current slide
    currentActiveSlide.classList.remove("fade");
    currentActiveSlide.classList.add("fade-out");

    // Wait for the fade-out to finish before switching slides
    setTimeout(() => {
      // Hide all slides and reset animation classes
      for (i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";
        slides[i].classList.remove("fade-out");
      }

      // Show the new active slide (remove .fade if you don't want fade-in)
      slides[slideIndex - 1].style.display = "block";
      slides[slideIndex - 1].classList.add("fade");

      // Update dot indicators
      for (i = 0; i < dots.length; i++) {
        dots[i].className = dots[i].className.replace(" active", "");
      }
      dots[slideIndex - 1].className += " active";

      // Restart auto-slide timer
      timer = setTimeout(showSlides, 4000);
    }, 1500); // Match the animation duration (1.5 seconds)
  } else {
    // Initial load: just show the first slide
    for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
    }
    slides[slideIndex - 1].style.display = "block";
    slides[slideIndex - 1].classList.add("fade");

    for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
    }
    dots[slideIndex - 1].className += " active";

    timer = setTimeout(showSlides, 4000);
  }
}

If you'd prefer new slides to appear instantly (no fade-in), just remove the line slides[slideIndex - 1].classList.add("fade"); from both the timeout block and the initial load section.


内容的提问来源于stack exchange,提问作者Kenken

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:21:45