没有动画的动画形式,保留在一个地方

时间:2018-11-26 02:10:27

标签: jquery html

是的,所以基本上我有一个动画形式,是从这里获得的:https://tympanus.net/codrops/2010/06/07/fancy-sliding-form-with-jquery/

从演示中可以看到,单击某个选项时,它应该滑到下一组选项。但是对我来说,在下面的代码中,当我单击一个选项时,它就保持静止。

  <div id="wrapper">
                <div id="steps">
                    <form id="formElem" name="formElem" action="" method="post">
                        <fieldset class="step">
                            <legend>Consultation</legend>
                            <p>
                                <input class = "consultation" type="submit" value="General Consultation">
                            </p>
                            <p>
                                <input class = "consultation" type="submit" value="Blood Test with Consultation">
                            </p>
                            <p>
                                <input class = "consultation" type="submit" value="Full Sexual Health Screen">
                            </p>
                             <p>
                                <input class = "consultation" type="submit" value="Hair Loss PRP Treatment">
                            </p>
                             <p>
                                <input class = "consultation" type="submit" value="Executive Health Screening">
                            </p>
                             <p>
                                <input class = "consultation" type="submit" value="Flu Vaccination">
                            </p>
                            <p>
                                <input class = "consultation" type="submit" value="Other">
                            </p>
                        </fieldset>

                        <fieldset class="step">
                            <legend>Provider</legend>
                             <p>
                                <input class = "provider" type="submit" value="Dr. Kevin O'Connell">
                            </p>
                             <p>
                                <input class = "provider" type="submit" value="Dr. Rachel Fitxgerald">
                            </p>
                             <p>
                                <input class = "provider" type="submit" value="Dr. Simon Gilmore">
                            </p>
                            <p>
                                <input class = "provider" type="submit" value="Nurse Julie Byrne">
                            </p>
                        </fieldset>
                        <fieldset class="step">
                            <legend>Date</legend>
                            <p>
                                <input type="date" name="date">
                            </p>
                             <p class="submit">
                                <button id="registerButton" type="submit">Confirm</button>
                            </p>
                        </fieldset>

                        <fieldset class="step">
                            <legend>Confirm</legend>
                            <p>
                                Thank you for requesting an appointment. We will be in touch with you shortly to confirm it.
                            </p>
                            <p class="submit">
                                <button id="registerButton"type="submit">Home</button>
                            </p>
                        </fieldset>
                    </form>
                </div>
                <div id="navigation" style="display:none;">
                    <ul>
                        <li class="">
                            <a href="#">Consultation</a>
                        </li>
                        <li>
                            <a href="#">Provider</a>
                        </li>
                        <li>
                            <a href="#">Date</a>
                        </li>
                        <li>
                            <a href="#">Confirm</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

JQUERY

$(function() {
    /*
    number of fieldsets
    */
    var fieldsetCount = $('#formElem').children().length;

    /*
    current position of fieldset / navigation link
    */
    var current     = 1;

    /*
    sum and save the widths of each one of the fieldsets
    set the final sum as the total width of the steps element
    */
    var stepsWidth  = 0;
    var widths      = new Array();
    $('#steps .step').each(function(i){
        var $step       = $(this);
        widths[i]       = stepsWidth;
        stepsWidth      += $step.width();
    });
    $('#steps').width(stepsWidth);

    /*
    to avoid problems in IE, focus the first input of the form
    */
    $('#formElem').children(':first').find(':input:first').focus(); 

    /*
    show the navigation bar
    */
    $('#navigation').show();

    /*
    when clicking on a navigation link 
    the form slides to the corresponding fieldset
    */
    $('#navigation a').bind('click',function(e){
        var $this   = $(this);
        var prev    = current;
        $this.closest('ul').find('li').removeClass('selected');
        $this.parent().addClass('selected');
        /*
        we store the position of the link
        in the current variable 
        */
        current = $this.parent().index() + 1;
        /*
        animate / slide to the next or to the corresponding
        fieldset. The order of the links in the navigation
        is the order of the fieldsets.
        Also, after sliding, we trigger the focus on the first 
        input element of the new fieldset
        If we clicked on the last link (confirmation), then we validate
        all the fieldsets, otherwise we validate the previous one
        before the form slided
        */
        $('#steps').stop().animate({
            marginLeft: '-' + widths[current-1] + 'px'
        },500,function(){
            if(current == fieldsetCount)
                validateSteps();
            else
                validateStep(prev);
            $('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();    
        });
        e.preventDefault();
    });

    /*
    clicking on the tab (on the last input of each fieldset), makes the form
    slide to the next step
    */
    $('#formElem > fieldset').each(function(){
        var $fieldset = $(this);
        $fieldset.children(':last').find(':input').keydown(function(e){
            if (e.which == 9){
                $('#navigation li:nth-child(' + (parseInt(current)+1) + ') a').click();
                /* force the blur for validation */
                $(this).blur();
                e.preventDefault();
            }
        });
    });

    /*
    validates errors on all the fieldsets
    records if the Form has errors in $('#formElem').data()
    */
    function validateSteps(){
        var FormErrors = false;
        for(var i = 1; i < fieldsetCount; ++i){
            var error = validateStep(i);
            if(error == -1)
                FormErrors = true;
        }
        $('#formElem').data('errors',FormErrors);   
    }

    /*
    validates one fieldset
    and returns -1 if errors found, or 1 if not
    */
    function validateStep(step){
        if(step == fieldsetCount) return;

        var error = 1;
        var hasError = false;
        $('#formElem').children(':nth-child('+ parseInt(step) +')').find(':input:not(button)').each(function(){
            var $this       = $(this);
            var valueLength = jQuery.trim($this.val()).length;

            if(valueLength == ''){
                hasError = true;
                $this.css('background-color','#FFEDEF');
            }
            else
                $this.css('background-color','#FFFFFF');    
        });
        var $link = $('#navigation li:nth-child(' + parseInt(step) + ') a');
        $link.parent().find('.error,.checked').remove();

        var valclass = 'checked';
        if(hasError){
            error = -1;
            valclass = 'error';
        }
        $('<span class="'+valclass+'"></span>').insertAfter($link);

        return error;
    }

    /*
    if there are errors don't allow the user to submit
    */
    $('#registerButton').bind('click',function(){
        if($('#formElem').data('errors')){
            alert('Please correct the errors in the Form');
            return false;
        }   
    });
});

1 个答案:

答案 0 :(得分:0)

为什么jQuery不对表单进行动画处理?

您没有导入jQuery库。

jQuery是一种API,默认情况下不包含在前端项目中,因此您需要在jQuery库中包含以下script元素。完成此操作后,您的项目现在可以访问jQuery库,并且可以为表单制作动画了。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

您可以了解有关脚本,它们的属性以及如何使用它们的更多信息here

Live Demo