`

一款漂亮的滑动表单

阅读更多
<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;
}

 

  • 大小: 31.7 KB
4
1
分享到:
评论
2 楼 guangqiang 2013-03-07  
lgy61 写道
给力,用起来不错的

互相学习!
1 楼 lgy61 2013-03-06  
给力,用起来不错的

相关推荐

    扁平式动画CSS3滑动开关按钮特效.zip

    这是一款扁平式动画CSS3滑动开关按钮特效,可以适应5种不同的场景,以及一种不可用的状态,非常的漂亮简洁。

    分享10款优雅动人的HTML5教程及

    今天我们要来分享10款优雅动人的HTML5教程及源码,这些HTML5教程都是我们这几天收集过来的,经过我们的挑选,希望能给大家带来帮助。...刚刚我们发布过一款CSS3开关切换滑动按钮,应该[......] 阅读全文&gt;&gt;

    JQuery&CSS;&CSS;+DIV实例大全.rar

    一款美化表单下拉列表、复选框等的jquery combobox插件实例 30.站长必备jquery实现combox自动筛选,高亮显示功能 4)菜单 1.jquery+CSS超炫丽横向多级滑动导航菜单 2.jQuery+CSS漂亮蓝色三级菜单下载 3....

    下拉框的表单美化插件,mSelect.js源代码.rar

    下拉框的表单美化插件,mSelect.js源代码,mSelect 是一个含有漂亮下拉框、漂亮按钮的表单美化插件,支持下拉框的滑动展开、折叠,原下拉的onchange事件,selected/disabled属性,支持上下键选择,支持表单的reset,...

    漂亮的选项卡切换注册表单js代码

    漂亮的选项卡切换注册表单js代码

    最新CSS3学习视频教程 33讲完整版 后盾网CSS3视频教程

    ├02 css3文本选择器 表单选择器 子选择器.mp4 ├03 rgba 字体单位 text-shadow box-shadow.mp4 ├04 盒子尺寸 box-sizing 分栏 background-size linear-gradient outline.mp4 ├05 利用media方式实现pc端响应式布局...

    mSelect 下拉框美化插件实例.rar

    mSelect 下拉框美化插件实例,mSelect 是一个含有漂亮下拉框、漂亮按钮的表单美化插件,支持下拉框的滑动展开、折叠,原下拉的onchange事件,selected/disabled属性,支持上下键选择,支持表单的reset,应该说该有的...

    为你的网站增加亮点的9款jQuery插件推荐

    Better Checkboxes一款简单的实现替代浏览器默认选择框的jQuery插件,可以帮助你丰富表单的表现形式,在JavaScript失效的时候回恢复为默认控件。 Thumbnails Navigation Gallery一个很漂亮的全屏滚动相册,效果很好...

    【S-CMS企业建站系统 v5.0 】闪灵CMS+含小程序+响应式布局+支持手机版网站+支持QQ旺旺客服

    闪灵CMS企业建站系统是一款专门为企业建站提供解决方案的产品,前端模板样式主打HTML5模板,以动画效果好、页面流畅、响应式布局为特色,程序主体采用PHP+MYSQL构架,拥有独立自主开发的一整套函数、标签系统,具有...

    自写JS下拉框美化插件(加入滑动展开、键盘选择等)

    内容索引:脚本资源,Ajax/JavaScript,下拉框,美化 Ay_自定义下拉框美化插件,功能强大,漂亮美观,支持表单reset、滑动展开动画、键盘上下键选择、onchange、selected disabled属性控制等,本插件有下拉的onchange...

    CSS商业网站布局之道

    本书是一本CSS技术专著,主要从布局这个角度全面、系统和深入地...除此之外,还就CSS开发中的一些技术难点和重点展开了详细的介绍,例如表格、表单、图像、超链接、导航菜单、滑动门、圆角、阴影、换肤、Tab技术等等。

    网趣商城网站源文件

    商品展示页面同样如此,如鼠标指向热门商品,可依次滑动展示商品的图片及价格信息,商品介绍、其他说明与顾客评论则鼠标点后在对应选项卡上展示,商品的4个大图鼠标可指向小图,上面自动显示大图,十分漂亮。...

    vc++ 开发实例源码包

    9:采用Messenger的弹出滑动消息提示框. 10:下载管理,虚拟文件夹. 11:自动ping. 12:连接到搜索引擎. 13:自动报告bug,建议等. 14:宏功能. 15:自动同步文件夹. 16:保存加载任务. 17:计划任务. 18:单线程下载时不能创建...

Global site tag (gtag.js) - Google Analytics