【问题标题】:Bootstrap Modal Backdrop only shows with "class=fade", not class="show"Bootstrap 模态背景仅显示“class=fade”,而不是 class="show"
【发布时间】:2016-09-14 19:14:52
【问题描述】:

我需要在我的页面启动时显示我的模态窗口,并且我能够在本网站的其他地方找到该解决方案(将 class="modal fade" 更改为 class="模态展示”)。

但是,在这样做之后,褪色的背景就消失了。我尝试在与“modal”相同的 div 上使用“modal-backdrop”类,但不透明度设置为 0.5,这使得背景和模态窗口都是透明的,这当然是不可取的。我对Javascript不太了解,所以问题可能出在那儿。

这是我的代码:

HTML:

   <div class="modal show"  tabindex="-1" role="dialog" data-keyboard="false" data-backdrop="static">
        <div class="modal-dialog" role="document">
            <div class="modal-content">

                <div class="modal-body">
                    <div>
                        <p style="font-size:17px;font-family: 'AntennaLight',  Arial, sans-serif;">Welcome to the <span style="font-family:'AntennaMedium',  Arial, sans-serif;">Lorem Ipsum</span> website.<br /> <br />
                        Please provide the ZIP code where you intend to register the vehicle and the intended date* you plan to purchase the vehicle.<br /> <br />
                        Thank you!<br /> <br />
                        </p>
                    </div>


                        <form id="msform" action='/Home/Submit' method="post">

                            <!-- fieldsets -->
                            <fieldset class="firstSet">

                                <p>
                                    <input type="text" name="zip" value="" placeholder="ZIP Code" />
                                </p>
                                <p>
                                    <input type="text" name="date" value="" placeholder="XX/XX/XXXX" />
                                </p>

                                <input name="submit" type="submit" class="submit action-button" value="Submit" />
                            </fieldset>
                        </form>

                  <div style="margin-top:30px;">

                    <p style="font-size:11px;font-family: 'AntennaLight',  Arial, sans-serif;">*If the sale date has already occurred, please use that date.</p>
                    </div>

                </div>
            </div>
        </div>
    </div>  

JS:

  <script>
   $( document ).ready(function() {
    $('.modal')
    .prop('class', 'modal') // revert to default
    .addClass( $(this).data('direction') );
  };
 </script>

在我工作过的其他网站上,使用 class="modal fade" 效果很好,当有一个按钮可以触发模式窗口时......但在这种情况下,我需要在页面启动时出现窗口和背景,只需将其更改为 class="modal show" 仅在启动时显示窗口,但没有背景。

谢谢!

【问题讨论】:

  • 所以你想要一些将你的 class="modal fade" 更改为模态显示的东西......当窗口加载时?
  • 我在窗口加载时出现了模态...但没有出现背景(模态背景类)。

标签: jquery html twitter-bootstrap


【解决方案1】:

您可以给模型一个 id 并像这样以模态方式启动它:

$( document ).ready(function() {
    $('#my-modal').modal('show')
})

这将保留您使用按钮手动触发时发生的行为。

【讨论】:

  • 我不确定我是否理解......所以这会取代我现在拥有的脚本?
  • 是的......有点:这是工作示例:jsfiddle.net/4h8L9ph4
  • 当页面准备好时,代码会导致模态框淡入。您只需要将 id="my-modal" 添加到 div 中,例如:
  • 如果顺序颠倒似乎确实会失败。因此,您需要添加 jquery,然后添加 bootstrap.js。尝试其他方法时出现这些错误:未捕获错误:Bootstrap 的 JavaScript 需要 jQuery bootstrap3-jquery.html:57 未捕获类型错误:$(...).modal 不是函数
  • 如果您使用的是引导程序 3.3.2,请使用 jquery 2.1.3
【解决方案2】:

在关闭模态的 div 标签后,您需要在类中添加一个带有模态背景淡入淡出的 div 标签,以获得背景外观。

还要确保在关闭模式时使用 jquery 将其删除。

Example:

<!-- Modal -->
<div id="myModal" class="modal show" role="dialog">
</div> <!-- end of modal -->

<div class="modal-backdrop fade in"></div>

Codepen 链接:

http://codepen.io/saa93/pen/amNpyg

【讨论】:

  • 您的 Codepen 没有显示带有 class="modal-backdrop fade"... 的附加
    标签...所以它不像您描述的那样工作。
猜你喜欢
相关资源
最近更新 更多
热门标签