ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

js实现progress-steps(小练习)

2022-03-20 17:03:47  阅读:228  来源: 互联网

标签:-- js color steps currentActive progress line border


使用javascript实现一个progress-steps巩固自己的基础知识。
在这里插入图片描述⭐️index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Progress Steps</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <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>

  <script src="script.js"></script>
</body>
</html>

⭐️布局基本使用flex布局,也用到了定位

/* 导入字体样式 */
@import url(https://fonts.googleapis.com/css?family=Muli&display=swap);

/* 定义css变量 */
:root {
  --line-border-fill: #3498db;
  --line-border-empty: #e0e0e0;
}

* {
  box-sizing: border-box;
}

body {
  background-color: #f6f7fb;
  font-family: 'Muli',sans-serif;
  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;

  /* 调整文字与按钮的距离 */
  margin-bottom: 30px;
  max-width: 100%;
  width: 350px;

  /* 子绝父相 */
  position: relative;
}

/* 进度条的直线 */
.progress-container::before {
  content: '';
  background-color: var(--line-border-empty);
  position: absolute;
  top: 50%;
  left: 0;
  /* y轴移动50% 放在文字中间 */
  transform: translateY(-50%);
  height: 4px;
  width: 100%;
  /* 仅在有定位的元素上生效 z-index:-1; 进度条在文字后面 */
  z-index: -1;
}

/* 点击事件写完后 点击width增大 或减小 */
.progress {
  background-color: var(--line-border-fill);
  position: absolute;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
  height: 4px;
  width: 0%;
  z-index: -1;
  /* 进度条过渡变颜色的时间 */
  transition: .4s ease;
}

/* 数字 */
.circle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 3px solid var(--line-border-empty);
  background-color: #fff;
  color: #999;
  /* 数字外的边框 变成蓝色的过渡时间 */
  transition: .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:disabled {
  background-color: var(--line-border-empty);
  cursor: not-allowed;
}

⭐️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()
})

function 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
  }
}

这个案例是在github看到的50个javascript项目
链接地址:50个javascript小项目

标签:--,js,color,steps,currentActive,progress,line,border
来源: https://blog.csdn.net/weixin_45732235/article/details/123616302

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有