前端css走马灯式图片轮播

DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Continuous Carouseltitle>
  <style>
    body {
      margin: 0;
      overflow: hidden;
    }

    #carousel-container {
      white-space: nowrap;
      overflow: hidden;
      position: relative;
    }

    .carousel-item {
      display: inline-block;
      margin-right: 5vw; /* 图片间隔,使用vw单位,可根据需要调整 */
    }

    .carousel-inner {
      display: inline-block;
      white-space: nowrap;
      animation: marquee 20s linear infinite; /* 调整滚动速度,例如20s */
    }

    @keyframes marquee {
      0% {
        transform: translateX(0);
      }
      100% {
        transform: translateX(-100%); /* 从左向右滚动 */
      }
    }
  style>
head>
<body>
  <div id="carousel-container">
    <div class="carousel-inner" id="sequence-1">div>
    <div class="carousel-inner" id="sequence-2">div>
  div>

  <script>
    document.addEventListener('DOMContentLoaded', () => {
      const container = document.getElementById('carousel-container');
      const sequence1 = document.getElementById('sequence-1');
      const sequence2 = document.getElementById('sequence-2');

      // 创建图片序列
      const createImageSequence = (target) => {
        for (let i = 1; i <= 7; i++) {
          const item = document.createElement('div');
          item.classList.add('carousel-item');
          const img = document.createElement('img');
          img.src = `image${i}.jpg`;
          img.alt = `Image ${i}`;
          item.appendChild(img);
          target.appendChild(item);
        }
      };

      // 插入两个图片序列
      createImageSequence(sequence1);
      createImageSequence(sequence2);

      // Optional: 调整滚动速度
      const duration = 20; // in seconds
      container.style.animationDuration = `${duration}s`;
    });
  script>
body>
html>

你可能感兴趣的:(前端,css)