Fancybox 在用于显示表单时不拉伸

Fancybox not stretching when using to display form

我想在 fancybox overlay 中显示一个简单的表单,它在较小的屏幕上也能很好地工作。我在这里设置了一个示例 http://design.imago.ee/test/fancybox-form/index1.html 最初我将表单宽度设置为 450px,在 620px 屏幕尺寸下我将表单宽度设置为 100%,完成后,fancybox window 在宽度方向折叠并且表单显示不正确。有趣的是,常规文本内容(示例中的第二个按钮)不会发生这种情况。我知道我可以通过媒体查询手动更改宽度,但这并不是一个好的解决方案。谁能帮忙?谢谢。

这不是一个很好的解决方案,但会给你一个想法,并可能让你更接近你的目标

fancybox 的变化css

.fancybox-inner {
    overflow: hidden !important;
}

脚本

计算 window 的宽度并调整 content 的宽度,使用 setInterval 以便动态调整宽度的任何变化。

setInterval(dimension, 100);

function dimension() {
    $('.content').css('width', $(window).width() * 0.6);
}

演示

$(document).ready(function () {
    $('.fancybox').fancybox({
        padding: 0,
        helpers: {
            overlay: {
                locked: false
            },
            title: {
                type: 'inside'
            }
        }
    });

    setInterval(dimension, 100);

    function dimension() {
        $('.content').css('width', $(window).width() * 0.6);
    }

});
.fancybox-inner {
    overflow: hidden !important;
}
* {
    margin: 0;
    padding: 0;
}
/* =========================== Layout styles =================== */
 body, html {
    height: 100%;
}
body {
    font: 14px/1.4'Open sans', sans-serif;
    overflow: hidden;
    background-color: #fff;
    padding: 20px 4%;
}
.centered-wrap {
    max-width: 1100px;
    margin: 0 auto;
    width: 100%;
}
a.fancybox {
    display: inline-block;
    vertical-align: top;
    background-color: #59a3d3;
    color: #fff;
    padding: 4px 7px;
    border-radius: 2px;
    text-decoration: none;
}
h1 {
    font-size: 23px;
    font-weight: 600;
    margin-bottom: 15px;
}
p {
    margin-bottom: 20px;
}
.content {
    padding: 20px 30px;
}
input[type="text"] {
    border: 1px solid #ccc;
    height: 30px;
    font: 13px/30px'Open sans', sans-serif;
    box-sizing: border-box;
    width: 100%;
    padding: 0 7px;
}
#form {
    width: 450px;
    padding: 30px;
}
#form .row {
    margin-bottom: 10px;
}
#form .col {
    float: left;
}
#form .col1 {
    width: 25%;
}
#form .col2 {
    width: 75%;
}
#form label {
    display: inline-block;
    vertical-align: top;
    padding: 6px 10px 0 0;
}
/* ======================= media queries ======================= */
 @media screen and (max-width: 620px) {
    #form {
        width: 100%;
        text-align: center;
        padding: 5px;
    }
    #form .col {
        float: none;
        width: auto;
        text-align: center;
        margin-right: 10px
    }
    #form label {
    }
}
/* ======================== clearfix =========================== */
/* Force Element To Self-Clear its Children */
 .clearfix:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content:" ";
    clear: both;
    height: 0;
}
.clearfix {
    display: inline-block;
}
/* start commented backslash hack \*/
 * html .clearfix {
    height: 1%;
}
.clearfix {
    display: block;
}
/* close commented backslash hack */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" />
<div class="centered-wrap">
    <p><a class="fancybox" href="#form">Fancybox with form</a></p>
    <div style="display: none;">
        <div id="form" class="content">
            <div class="row clearfix">
                <div class="col col1">
                    <label>Form label</label>
                </div>
                <div class="col col2">
                    <input type="text">
                </div>
            </div>
            <div class="row clearfix">
                <div class="col col1">
                    <label>Form label</label>
                </div>
                <div class="col col2">
                    <input type="text">
                </div>
            </div>
            <div class="row clearfix">
                <div class="col col1">
                    <label>Form label</label>
                </div>
                <div class="col col2">
                    <input type="text">
                </div>
            </div>
        </div>
    </div>
</div>

Fiddle Example

注意:调整 fiddle 视图屏幕以查看表单宽度如何随着屏幕尺寸的变化而自行调整。