结帐径向进度条事件

时间:2017-01-03 06:23:37

标签: javascript jquery css progress-bar checkout

我已经通过一些动画和过渡创建了径向结账进度条。在按钮上添加了事件。但问题是我在结账时的每一步都有不同的内容。什么是最佳做法。是否更好地使用数据attr。为了这。内容应隐藏并显示以进行特定结帐。 codepen

    <div class="step-1" id="checkout-progress" data-current-step="1">
    <div class="progress-bar">
        <div class="step step-1 active " data-step="1"><span> 1</span>
            <div class="step-check">t</div>
            <div class="step-label"> address</div>
        </div>
        <div class="step step-2" data-step="2"><span> 2</span>
            <div class="step-check">a</div>
            <div class="step-label"> shipping</div>
        </div>
        <div class="step step-3" data-step="3"><span> 3</span>
            <div class="step-check">b</div>
            <div class="step-label"> payment</div>
        </div>
        <div class="step step-4" data-step="4"><span> 4</span>
            <div class="step-check">3</div>
            <div class="step-label"> summary</div>
        </div>
    </div>
</div>
<!-- <div class="button-container">
    <div class="btn btn-prev"> previous step</div>
    <div class="btn btn-next"> next step</div>
</div> -->


<div class="checkout-content" data-step="1">
    <h1>checkout content 1</h1>
    <div class="btn btn-next"> next step</div>
    <div class="btn btn-next"> next step</div>
</div>
<div class="checkout-content" data-step="2">
    <h1>checkout content 2</h1>
    <div class="btn btn-next"> next step</div>
    <div class="btn btn-next"> next step</div>
</div>
<div class="checkout-content" data-step="3">
    <h1>checkout content 3</h1>
    <div class="btn btn-next"> next step</div>
    <div class="btn btn-next"> next step</div>
</div>
<div class="checkout-content" data-step="4">
    <h1>checkout content 4</h1>
    <div class="btn btn-next"> next step</div>
    <div class="btn btn-next"> next step</div>
</div>


$('.btn-next').on('click', function() {

    var currentStepNum = $('#checkout-progress').data('current-step');
    var nextStepNum = (currentStepNum + 1);
    var currentStep = $('.step.step-' + currentStepNum);
    var nextStep = $('.step.step-' + nextStepNum);
    var progressBar = $('#checkout-progress');
    $('.btn-prev').removeClass('disabled');
    if(currentStepNum == 5) {
        return false;
    }
    if(nextStepNum == 5){
        $(this).addClass('disabled');
    }
    // $('.checkout-progress').removeClass('.step-' + currentStepNum).addClass('.step-' + (currentStepNum + 1));

    currentStep.removeClass('active').addClass('valid');
    currentStep.find('span').addClass('opaque');
    currentStep.find('.step-check').removeClass('opaque');

    nextStep.addClass('active');
    progressBar.removeAttr('class').addClass('step-' + nextStepNum).data('current-step', nextStepNum);
});


$('.btn-prev').on('click', function() {

    var currentStepNum = $('#checkout-progress').data('current-step');
    var prevStepNum = (currentStepNum - 1);
    var currentStep = $('.step.step-' + currentStepNum);
    var prevStep = $('.step.step-' + prevStepNum);
    var progressBar = $('#checkout-progress');
    $('.btn-next').removeClass('disabled');
    if(currentStepNum == 1) {
        return false;
    }
    if(prevStepNum == 1){
        $(this).addClass('disabled');
    }
    // $('.checkout-progress').removeClass('.step-' + currentStepNum).addClass('.step-' + (prevStepNum));

    currentStep.removeClass('active');
    prevStep.find('span').removeClass('opaque');
    prevStep.find('.step-check').addClass('opaque');

    prevStep.addClass('active').removeClass('valid');
    progressBar.removeAttr('class').addClass('step-' + prevStepNum).data('current-step', prevStepNum);
});

1 个答案:

答案 0 :(得分:0)

为什么要在每个阶段的“包含”部分中重复“操作”(下一个&amp; pev)按钮。创建一个名为.actions的新部分,然后你可以将你的动作按钮放在他们的... 然后你可以拥有data-target属性,该属性保存了点击时需要去的stepNumber的值......

每次点击您希望用户下次移动时,您需要更改data-target属性...还有pev按钮...