制作html css 步骤进度条(完整代码)

这个动画步骤进度条的css制作的非常简单。那里有两个按钮可以控制步骤,它们将逐步进行。我在这个多步骤进度条 css 中使用了 4 个步骤。如果你愿意,你可以使用更多。

我使用了一些 javascript 来创建这一步进度条。横步进度条主要用在注册表中。以下是在 HTML 中创建步骤栏的步骤:

 

  1. 创建一个 HTML 文件并添加以下结构:
     
    <div class="container">
          <div class="progress-container">
            <div class="progress" id="progress"></div>
            <div class="circle active">1</div>
            <div class="circle">2</div>
            <div class="circle">3</div>
            <div class="circle">4</div>
          </div>
          <button class="btn" id="prev" disabled>Prev</button>
          <button class="btn" id="next">Next</button>
        </div>

  2. 添加以下 CSS 来设置步进栏的样式:
     
    @import url("https://fonts.googleapis.com/css2?family=Muli&display=swap");
    
    :root {
      --line-border-fill: #3498db;
      --line-border-empty: #e0e0e0;
    }
    
    * {
      box-sizing: border-box;
    }
    
    body {
      font-family: "Muli", sans-serif;
      background-color: #f6f7fb;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100vh;
      overflow: hidden;
      margin: 0;
    }
    
    .container {
      text-align: center;
    }
    
    .progress-container {
      display: flex;
      justify-content: space-between;
      position: relative;
      margin-bottom: 30px;
      max-width: 100%;
      width: 350px;
    }
    
    .progress-container::before {
      content: ""; /* Mandatory with ::before */
      background-color: var(--line-border-empty);
      position: absolute;
      top: 50%;
      left: 0;
      transform: translateY(-50%);
      height: 4px;
      width: 100%;
      z-index: -1;
    }
    
    .progress {
      background-color: var(--line-border-fill);
      position: absolute;
      top: 50%;
      left: 0;
      transform: translateY(-50%);
      height: 4px;
      width: 0%;
      z-index: -1;
      transition: 0.4s ease;
    }
    
    .circle {
      background-color: #fff;
      color: #999;
      border-radius: 50%;
      height: 30px;
      width: 30px;
      display: flex;
      align-items: center;
      justify-content: center;
      border: 3px solid var(--line-border-empty);
      transition: 0.4s ease;
    }
    
    .circle.active {
      border-color: var(--line-border-fill);
    }
    
    .btn {
      background-color: var(--line-border-fill);
      color: #fff;
      border: 0;
      border-radius: 6px;
      cursor: pointer;
      font-family: inherit;
      padding: 8px 30px;
      margin: 5px;
      font-size: 14px;
    }
    
    .btn:active {
      transform: scale(0.98);
    }
    
    .btn:focus {
      outline: 0;
    }
    
    .btn:disabled {
      background-color: var(--line-border-empty);
      cursor: not-allowed;
    }

  3. 添加 JavaScript 代码来控制步进栏行为:
     
    const progress = document.getElementById("progress");
    const prev = document.getElementById("prev");
    const next = document.getElementById("next");
    const circles = document.querySelectorAll(".circle");
    
    let currentActive = 1;
    
    next.addEventListener("click", () => {
      currentActive++;
      if (currentActive > circles.length) currentActive = circles.length;
      update();
    });
    
    prev.addEventListener("click", () => {
      currentActive--;
      if (currentActive < 1) currentActive = 1;
      update();
    });
    
    const update = () => {
      circles.forEach((circle, index) => {
        if (index < currentActive) circle.classList.add("active");
        else circle.classList.remove("active");
      });
      const actives = document.querySelectorAll(".active");
      progress.style.width =
        ((actives.length - 1) / (circles.length - 1)) * 100 + "%";
      if (currentActive === 1) prev.disabled = true;
      else if (currentActive === circles.length) next.disabled = true;
      else {
        prev.disabled = false;
        next.disabled = false;
      }
    };

这将创建一个带有进度条和圆圈元素的基本步骤条来表示每个步骤。上一个和下一个按钮可用于在步骤之间导航,步骤栏的外观将根据当前活动步骤动态更新。

在这个例子中,步骤进度条由一个进度条和一系列用文本表示的步骤组成。进度条将根据当前活动的步骤改变宽度,步骤的外观将根据它们是否处于活动状态而改变。您可以根据需要调整宽度、颜色和字体大小值,以自定义步骤进度条的外观。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是制作轮播图的详细步骤以及完整的HTML+CSS+JS代码: 步骤: 1. 创建HTML结构,包括一个容器元素和多个轮播项元素。 2. 使用CSS样式设置容器元素的宽度和高度,并隐藏溢出部分。 3. 使用CSS样式设置轮播项元素的宽度和高度,并设置为浮动或使用flex布局。 4. 创建一个JavaScript函数,用于实现轮播功能。 5. 在JavaScript函数中,获取容器元素和轮播项元素,并设置初始索引值。 6. 在JavaScript函数中,使用定时器或事件监听器来触发轮播动画。 7. 在轮播动画中,根据索引值切换轮播项元素的显示。 8. 在轮播动画中,使用animate函数来实现平滑的过渡效果。 9. 可选:添加导航按钮或指示器来控制轮播。 HTML+CSS+JS代码如下: ```html <!DOCTYPE html> <html> <head> <title>轮播图制作</title> <style> .carousel-container { width: 600px; height: 400px; overflow: hidden; position: relative; } .carousel-item { width: 100%; height: 100%; float: left; display: none; } </style> </head> <body> <div class="carousel-container"> <div class="carousel-item"> <img src="image1.jpg" alt="Image 1"> </div> <div class="carousel-item"> <img src="image2.jpg" alt="Image 2"> </div> <div class="carousel-item"> <img src="image3.jpg" alt="Image 3"> </div> </div> <script> function animate(obj, target, callback) { clearInterval(obj.timer); obj.timer = setInterval(function () { var step = (target - obj.offsetLeft) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); if (obj.offsetLeft == target) { clearInterval(obj.timer); if (callback) { callback(); } } obj.style.left = obj.offsetLeft + step + 'px'; }, 15); } window.onload = function () { var container = document.querySelector('.carousel-container'); var items = document.querySelectorAll('.carousel-item'); var index = 0; function carousel() { for (var i = 0; i < items.length; i++) { items[i].style.display = 'none'; } items[index].style.display = 'block'; animate(items[index], 0, function () { setTimeout(function () { animate(items[index], -container.offsetWidth); index = (index + 1) % items.length; }, 2000); }); } carousel(); }; </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值