添加下一步按钮到步骤加载栏

我有这个加载栏。当您添加active到时div class="step",它会增加进度。如何添加下一个和上一个按钮,根据您所在的步骤激活该按钮。


请帮忙!


$(document).ready(function() {

  $('.step').each(function(index, element) {

    // element == this

    $(element).not('.active').addClass('done');

    $('.done').html('<i class="icon-ok"></i>');

    if ($(this).is('.active')) {

      return false;

    }

  });

});

<!DOCTYPE html>

<html>


<head>

  <meta charset="UTF-8">

  <title>CodePen - loadingbar</title>

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  <link rel="stylesheet" href="./style.css">

  <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>


</head>


<body>

  <!-- partial:index.partial.html -->

  <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">

  <script src="https://kit.fontawesome.com/a076d05399.js"></script>

  <div id="steps">

    <div class="step active" data-desc="Listing information"><i class="far fa-hand-pointer"></i></div>

    <div class="step" data-desc="Photos & Details"><i class="fas fa-mortar-pestle"></i></div>

    <div class="step" data-desc="Review & Post"><i class="fas fa-shipping-fast"></i></div>

    <div class="step" data-desc="Your order"><i class="fas fa-shopping-cart"></i></div>

  </div>


  <!--

  Try adding the active class to another 'step'

  to see what's going on :)

-->

  <!-- partial -->

  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

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


</body>


</html>


牛魔王的故事
浏览 133回答 1
1回答

慕雪6442864

检查下面的next()和previous()功能,我认为它们可以实现您的目标。另外,我不清楚你的$(document).ready()功能是如何必要的,也可能不是。$(document).ready(function() {&nbsp; $('.step').each(function(index, element) {&nbsp; &nbsp; // element == this&nbsp; &nbsp; $(element).not('.active').addClass('done');&nbsp; &nbsp; $('.done').html('<i class="icon-ok"></i>');&nbsp; &nbsp; if ($(this).is('.active')) {&nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; });&nbsp; $('.step>i.icon-ok').hide();});const iconClasses = ['far fa-hand-pointer', 'fas fa-mortar-pestle', 'fas fa-shipping-fast', 'fas fa-shopping-cart'];function next() {&nbsp; //console.log("Next", Math.random());&nbsp; let latestActiveStep = $("div.step.active").not(".done");&nbsp; let stepNumber = +$(latestActiveStep).data("stepnum");&nbsp; console.log("step", stepNumber);&nbsp; $(latestActiveStep).addClass("done");&nbsp; $(latestActiveStep).find("i.icon-ok").toggle();&nbsp; $(latestActiveStep).find("i").not(".icon-ok").toggle();&nbsp; $(latestActiveStep).next().addClass("active");}function previous() {&nbsp; //console.log("Prev", Math.random());&nbsp; let latestDoneStep = $("div.step.active.done").last();&nbsp; let stepNumber = +$(latestDoneStep).data("stepnum");&nbsp; console.log("step", stepNumber);&nbsp; $(latestDoneStep).removeClass("done");&nbsp; $(latestDoneStep).next().removeClass("active");&nbsp; $(latestDoneStep).find("i.icon-ok").toggle();&nbsp; $(latestDoneStep).find("i").not(".icon-ok").toggle();}@import url(https://fonts.googleapis.com/css?family=Open+Sans:300,400,700,600);body {&nbsp; background-color: #e6e6e6;&nbsp; font-family: 'Open Sans', sans-serif;}#steps {&nbsp; width: 505px;&nbsp; margin: 50px auto;}.step {&nbsp; width: 40px;&nbsp; height: 40px;&nbsp; background-color: white;&nbsp; display: inline-block;&nbsp; border: 4px solid;&nbsp; border-color: transparent;&nbsp; border-radius: 50%;&nbsp; color: #cdd0da;&nbsp; font-weight: 600;&nbsp; text-align: center;&nbsp; line-height: 35px;}.step:first-child {&nbsp; line-height: 40px;}.step:nth-child(n+2) {&nbsp; margin: 0 0 0 100px;&nbsp; transform: translate(0, -4px);}.step:nth-child(n+2):before {&nbsp; width: 75px;&nbsp; height: 3px;&nbsp; display: block;&nbsp; background-color: white;&nbsp; transform: translate(-95px, 21px);&nbsp; content: '';}.step:after {&nbsp; width: 150px;&nbsp; display: block;&nbsp; transform: translate(-55px, 3px);&nbsp; color: #818698;&nbsp; content: attr(data-desc);&nbsp; font-weight: 400;&nbsp; font-size: 13px;}.step:first-child:after {&nbsp; transform: translate(-55px, -1px);}.step.active {&nbsp; border-color: #73b5e8;&nbsp; color: #73b5e8;}.step.active:before {&nbsp; background: linear-gradient(to right, #58bb58 0%, #73b5e8 100%);}.step.active:after {&nbsp; color: #73b5e8;}.step.done {&nbsp; background-color: #58bb58;&nbsp; border-color: #58bb58;&nbsp; color: white;}.step.done:before {&nbsp; background-color: #58bb58;}<!DOCTYPE html><html><head>&nbsp; <meta charset="UTF-8">&nbsp; <title>CodePen - loadingbar</title>&nbsp; <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">&nbsp; <link rel="stylesheet" href="./style.css">&nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script></head><body>&nbsp; <!-- partial:index.partial.html -->&nbsp; <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">&nbsp; <script src="https://kit.fontawesome.com/a076d05399.js"></script>&nbsp; <div id="steps">&nbsp; &nbsp; <div class="step active" data-stepnum="0" data-desc="Listing information"><i class="far fa-hand-pointer"></i><i class="icon-ok"></i></div>&nbsp; &nbsp; <div class="step" data-stepnum="1" data-desc="Photos & Details"><i class="fas fa-mortar-pestle"></i><i class="icon-ok"></i></div>&nbsp; &nbsp; <div class="step" data-stepnum="2" data-desc="Review & Post"><i class="fas fa-shipping-fast"></i><i class="icon-ok"></i></div>&nbsp; &nbsp; <div class="step" data-stepnum="3" data-desc="Your order"><i class="fas fa-shopping-cart"></i><i class="icon-ok"></i></div>&nbsp; </div>&nbsp; <button onclick="previous()">Previous</button>&nbsp; <button onclick="next()">Next</button>&nbsp; <!--&nbsp; Try adding the active class to another 'step'&nbsp; to see what's going on :)-->&nbsp; <!-- partial -->&nbsp; <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>&nbsp; <script src="./script.js"></script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript