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

基于W3 CSS滑块定制四图组自动定时轮播的技术需求

Got it, let's build that 4-image per group auto-rotating slider you're looking for! This implementation uses vanilla HTML/CSS/JS (no external libraries) and matches your requirement of switching between groups of 4 images on a timer, with smooth transitions.

HTML Structure

First, set up the basic structure: a container to hide overflow, a wrapper to hold all image groups, and individual groups each containing 4 images. Just replace the src values with your actual image paths.

<div class="slider-container">
  <div class="slider-wrapper">
    <!-- Group 1 -->
    <div class="image-group">
      <img src="img/1.jpg" alt="Image 1">
      <img src="img/2.jpg" alt="Image 2">
      <img src="img/3.jpg" alt="Image 3">
      <img src="img/4.jpg" alt="Image 4">
    </div>
    <!-- Group 2 -->
    <div class="image-group">
      <img src="img/5.jpg" alt="Image 5">
      <img src="img/6.jpg" alt="Image 6">
      <img src="img/7.jpg" alt="Image 7">
      <img src="img/8.jpg" alt="Image 8">
    </div>
    <!-- Group 3 -->
    <div class="image-group">
      <img src="img/9.jpg" alt="Image 9">
      <img src="img/10.jpg" alt="Image 10">
      <img src="img/11.jpg" alt="Image 11">
      <img src="img/12.jpg" alt="Image 12">
    </div>
  </div>
</div>
CSS Styling

Next, style the slider to ensure each group takes up the full container width, images are evenly spaced, and transitions are smooth.

.slider-container {
  width: 100%;
  max-width: 1200px; /* Adjust this to fit your layout */
  overflow: hidden;
  margin: 0 auto;
}

.slider-wrapper {
  display: flex;
  transition: transform 0.5s ease-in-out; /* Smooth slide animation */
}

.image-group {
  display: flex;
  width: 100%; /* Each group fills the entire container */
  gap: 10px; /* Space between images—tweak as needed */
}

.image-group img {
  width: calc(25% - 7.5px); /* 4 images per row, accounting for gap */
  height: auto;
  object-fit: cover; /* Preserve image aspect ratio (use 'contain' to show full image) */
  border-radius: 4px; /* Optional: add rounded corners */
}
JavaScript Logic

Finally, add the auto-rotation logic to switch groups on a timer. I've also included optional pause-on-hover functionality for better user experience.

const sliderWrapper = document.querySelector('.slider-wrapper');
const imageGroups = document.querySelectorAll('.image-group');
const totalGroups = imageGroups.length;
let currentGroupIndex = 0;
const slideInterval = 3000; // Switch every 3 seconds (adjust as needed)

// Function to slide to the next group
function switchToNextGroup() {
  currentGroupIndex = (currentGroupIndex + 1) % totalGroups;
  // Shift the wrapper horizontally by -100% per group
  sliderWrapper.style.transform = `translateX(-${currentGroupIndex * 100}%)`;
}

// Start auto-sliding
let slideTimer = setInterval(switchToNextGroup, slideInterval);

// Optional: Pause slider when hovering over it
const sliderContainer = document.querySelector('.slider-container');
sliderContainer.addEventListener('mouseenter', () => {
  clearInterval(slideTimer);
});

sliderContainer.addEventListener('mouseleave', () => {
  slideTimer = setInterval(switchToNextGroup, slideInterval);
});
Quick Customization Tips
  • Change slideInterval in the JS to adjust how fast groups switch (value is in milliseconds).
  • Modify the gap in .image-group to adjust spacing between images.
  • Swap object-fit: cover with object-fit: contain if you want to display full images without cropping.
  • Add more image-group divs if you have additional sets of 4 images to include.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:52:14