响应式jquery步骤

时间:2013-10-15 22:53:23

标签: javascript jquery css

我正在尝试实现jquery-steps,并且大多数情况下它都进展顺利。我遇到的一个问题是步骤选项卡没有完全响应。

我正在做横向实现。他们使用窗口调整大小,但是它们不会像在示例中那样堆叠,所以最终我会找到一些非常小的,不可读的步骤选项卡。

不幸的是,开发者网站上的所有示例都在同一页面上,因此尝试确定要使用的css有点问题。

任何帮助将不胜感激:

<!DOCTYPE html>
<!--[if lt IE 7]>      <html lang="en" xmlns="http://www.w3.org/1999/xhtml" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html lang="en" xmlns="http://www.w3.org/1999/xhtml" class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html lang="en" xmlns="http://www.w3.org/1999/xhtml" class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" class="no-js"> <!--<![endif]-->
    <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width" />
    <title>Page Title</title>
    <link rel="stylesheet" href="css/normalize.css" />
    <link href="css/hwr.css" rel="stylesheet" media="screen" />
    <link href="Content/jquery.steps.css" rel="stylesheet" />
    <link href="Content/bootstrap.min.css" rel="stylesheet" />

    <script src="Scripts/jquery-2.0.3.min.js"></script>
    <script src="Scripts/jquery.cookie.js"></script>
    <script src="Scripts/jquery.steps.js"></script>
    <script src="Scripts/jquery.validate.min.js"></script>
    <script src="Scripts/bootstrap.min.js"></script>
    <script src="Scripts/modernizr-2.6.2.js"></script>

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
    <script src="js/html5Shiv/html5shiv.js"></script>
    <script src="js/respond/respond.min.js"></script>
<![endif]-->


<script type='text/javascript'>

    $(document).ready(function () {
        $.validator.addMethod("postalCodeValid", function (validationPostalCode, element) {
            return (this.optional(element) || validationPostalCode.startsWith("85"));
            }, "You must be located in an '85' postal code to participate.");


        if (typeof String.prototype.startsWith != 'function') {
            String.prototype.startsWith = function (str) {
                return this.slice(0, str.length) == str;
            };
        }

        function errorPlacement(error, element) {
            element.before(error);

            element.popover({
                content: error.text(),
                placement: function () {
                    return (element.parents(".content").width() >= 550) ? "right" : "top";
                },
                trigger: "focus hover"
            });

            $(".popover-content", element.next(".popover")).empty().prepend(error);
        }

        $("#form").steps({
            headerTag: "h1",
            bodyTag: "fieldset",
            stepsOrientation: "horizontal",
            transitionEffect: $.fn.steps.transitionEffect.fade,
            transitionEffectSpeed: 400,
            onStepChanging: function (event, currentIndex, newIndex) {
                // Always allow going backward even if the current step contains invalid fields!
                if (currentIndex > newIndex) {
                    return true;
                }

                if (currentIndex == 0) {

                }


                // Forbid suppressing "Warning" step if the user is to young
                if (newIndex === 3 && Number($("#age").val()) < 18) {
                    return false;
                }

                var form = $(this);

                // Clean up if user went backward before
                if (currentIndex < newIndex) {
                    // To remove error styles
                    $(".body:eq(" + newIndex + ") label.error", form).remove();
                    $(".body:eq(" + newIndex + ") .error", form).removeClass("error");
                }

                // Disable validation on fields that are disabled or hidden.
                form.validate().settings.ignore = ":disabled,:hidden";

                // Start validation; Prevent going forward if false
                   return form.valid();
            },
            onStepChanged: function (event, currentIndex, priorIndex) {
                // Suppress (skip) "Warning" step if the user is old enough.
                if (currentIndex === 2 && Number($("#age").val()) >= 18) {
                    $(this).steps("next");
                }

                // Suppress (skip) "Warning" step if the user is old enough and wants to the previous step.
                if (currentIndex === 2 && priorIndex === 3) {
                    $(this).steps("previous");
                }
            },
            onFinishing: function (event, currentIndex) {
                var form = $(this);

                // Disable validation on fields that are disabled.
                // At this point it's recommended to do an overall check (mean ignoring only disabled fields)
                form.validate().settings.ignore = ":disabled";

                // Start validation; Prevent form submission if false
                return form.valid();
            },
            onFinished: function (event, currentIndex) {
                var form = $(this);

                // Submit form input
                form.submit();
            }
        }).validate({
            errorPlacement: function (error, element) {
                if (element.attr("name") == "isOfAge") {
                    error.insertAfter("#lblIsOfAge");
                }
                else {
                    error.insertBefore(element);
                }
            },
            rules: {
                confirm: {
                    equalTo: "#password"
                },
                isOfAge: {
                    equalTo: "#isOfAgeYes"
                },
                verifyPostalCode: {
                    minlength: 5,
                    number: true,
                    postalCodeValid: true
                }
            },
                messages: {
                    isOfAge: "You must be at least 21 to participate."
                }
            })
        });

</script>

</head>
<body>
    <div class="container">
    <div class="row">
        <div class="span12">
            <form id="form" action="#">
                <h1>Eligibility</h1>
                <fieldset>
                    <legend>Eligibility Confirmation</legend>

                    <div style="width: 65%; font-size: larger; font-weight: bold;">
                        YOU MUST BE 21 OR OVER TO PARTICIPATE
                    </div>
                    <br /><br />
                    <label id="lblIsOfAge" style="color: red; font-size: larger; font-weight: bold;" for="isOfAge">Are you at least 21 years of age?</label>
                    <br />
                    <label><input type="radio" name="isOfAge" id="isOfAgeYes" value="yes" title="Yes" style="width: 40px;" /> Yes</label>
                    <label><input type="radio" name="isOfAge" id="isOfAgeNo" value="no" checked="checked" title="No" style="width: 40px;" /> No</label>
                    <br />
                    <br />
                    <br />

                    <div style="width: 65%; font-size: larger; font-weight: bold;">
                        Please enter the zip code of your current location.
                    </div>
                    <br />
                    <label for="verifyPostalCode">Current Zip Code *</label>
                    <input id="verifyPostalCode" name="verifyPostalCode" type="text" class="required" />
                    <p>(*) Mandatory</p>
                </fieldset>

                <h1>Profile</h1>
                <fieldset>
                    <legend>Profile Information</legend>

                    <label for="name">First name *</label>
                    <input id="name" name="name" type="text" class="required" />
                    <label for="surname">Last name *</label>
                    <input id="surname" name="surname" type="text" class="required" />
                    <label for="email">Email *</label>
                    <input id="email" name="email" type="text" class="required email" />
                    <label for="address">Address</label>
                    <input id="address" name="address" type="text" />
                    <label for="age">Age (The warning step will show up if age is less than 18) *</label>
                    <input id="age" name="age" type="text" class="required number" />
                    <p>(*) Mandatory</p>
                </fieldset>

                <h1>Payment</h1>
                <fieldset>
                    <legend>Payment Info & Billing Address</legend>

                </fieldset>

                <h1>Tickets</h1>
                <fieldset>
                    <legend>Ticket specification</legend>

                </fieldset>

                <h1>Summary</h1>
                <fieldset>
                    <legend>Purcahse Summary and Approval</legend>

                </fieldset>

            </form>
        </div>
    </div>
    </div>
</body>
</html>

另外,在jquery.steps.css中我修改了这个:

.wizard > .steps > ul > li
{
    width: 25%;
}

到此:

.wizard > .steps > ul > li
{
    width: 20%;
}

保持第5步。

感谢您的任何见解。

JP

1 个答案:

答案 0 :(得分:13)

您正在使用的默认CSS只是一个很好的起点(意味着它可以实现在任何地方工作并且隔离良好,这样您就可以轻松地将其嵌入任何类型的项目中 - 甚至可以嵌入到现有项目中)。

我在项目网站(http://www.jquery-steps.com)上所做的是为我的网站量身定制的。无论如何,我没有问题为你提供我的特定CSS,使响应的事情发生。

在以下CSS代码段中,您会看到两组所谓的媒体查询,以便为平板电脑或手机等小型设备提供丰富的用户体验。

@media (max-width: 600px)
{
    .wizard > .steps > ul > li
    {
        width: 50%;
    }

    .wizard > .steps a,
    .wizard > .steps a:hover,
    .wizard > .steps a:active
    {
        margin-top: 0.5em;
    }

    .wizard.vertical > .steps,
    .wizard.vertical > .actions
    {
        display: block;
        float: none;
        width: 100%;
    }

    .wizard.vertical > .content
    {
        display: block;
        float: none;
        margin: 0 0.5em 0.5em;
        width: auto;
    }
}

@media (max-width: 480px)
{
    .wizard > .steps > ul > li
    {
        width: 100%;
    }
}

我希望这会对你有所帮助。如果没有,请随时回复我。

圣拉斐尔