欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一款漂亮的滑动表单

程序员文章站 2022-03-02 11:18:18
...
<h1>Fancy Sliding Form with jQuery</h1>
<div id="wrapper">
    <div id="steps">
        <form id="formElem" name="formElem" action="" method="post">
            <fieldset class="step">
                <legend>Account</legend>
                <p>
                    <label for="username">User name</label>
                    <input id="username" name="username" />
                </p>
                <p>
                    <label for="email">Email</label>
                    <input id="email" name="email" type="email" />
                </p>
                <p>
                    <label for="password">Password</label>
                    <input id="password" name="password" type="password" />
                </p>
            </fieldset>
            <fieldset>
            ...
            </fieldset>
        </form>
    </div>
    <div id="navigation" style="display:none;">
        <ul>
            <li class="selected">
                <a href="#">Account</a>
            </li>
            <li>
                <a href="#">Personal Details</a>
            </li>
            <li>
                <a href="#">Payment</a>
            </li>
            <li>
                <a href="#">Settings</a>
            </li>
            <li>
                <a href="#">Confirm</a>
            </li>
        </ul>
    </div>
</div>

 

$(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;
        }
    });
});

 

#navigation{
    height:45px;
    background-color:#e9e9e9;
    border-top:1px solid #fff;
    -moz-border-radius:0px 0px 10px 10px;
    -webkit-border-bottom-left-radius:10px;
    -webkit-border-bottom-right-radius:10px;
    border-bottom-left-radius:10px;
    border-bottom-right-radius:10px;
}
#navigation ul{
    list-style:none;
    float:left;
    margin-left:22px;
}
#navigation ul li{
    float:left;
    border-right:1px solid #ccc;
    border-left:1px solid #ccc;
    position:relative;
    margin:0px 2px;
}

 

#wrapper{
    -moz-box-shadow:0px 0px 3px #aaa;
    -webkit-box-shadow:0px 0px 3px #aaa;
    box-shadow:0px 0px 3px #aaa;
    -moz-border-radius:10px;
    -webkit-border-radius:10px;
    border-radius:10px;
    border:2px solid #fff;
    background-color:#f9f9f9;
    width:600px;
    overflow:hidden;
}
#steps{
    width:600px;
    overflow:hidden;
}
.step{
    float:left;
    width:600px;
}

 一款漂亮的滑动表单
            
    
    博客分类: JavaScript/Jquery htmlcssjqueryjsweb 

  • 一款漂亮的滑动表单
            
    
    博客分类: JavaScript/Jquery htmlcssjqueryjsweb 
  • 大小: 31.7 KB