简体   繁体   English

模态引导/一页多模态/模态开放类

[英]Modal Bootstrap / Multiple modal on one-page / modal-open Class

I've got a serious issue with modal from Bootstrap.我对 Bootstrap 的模态有一个严重的问题。

When I open a modal box in my website, there's absolutly no problem (the modal-open class is correctly added on the body) the modal is correct, shade ok and content clear :当我在我的网站上打开一个模态框时,绝对没有问题(模态打开类已正确添加到主体上)模态正确,阴影正常且内容清晰:

<button type="button" data-toggle="modal" data-target="#Modal_GA">buttonOpenMe</button>

When I close it manualy, it's ok too :当我手动关闭它时,也可以:

<button type="button" class="close2" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span><p class="TxtClose">CLOSE</p>

In my first modal, I've got two others buttons to check my previous & next projects (my website is a one-page).在我的第一个模式中,我还有另外两个按钮来检查我以前和下一个项目(我的网站是单页的)。
And this is what i want to do :这就是我想要做的:
action 1) open modal#1动作 1) 打开模态#1
action 2) scroll & click on next project行动 2) 滚动并点击下一个项目
action 3) close modal#1行动 3) 关闭模式#1
action 4) open modal#2, etc...动作 4) 打开 modal#2 等...

<button type="button" data-dismiss="modal" data-toggle="modal" data-target="#Modal_Animations"> CloseThis </button>

My body had an added class called "modal-open" when a modal box is opened.当打开模态框时,我的身体有一个名为“modal-open”的附加类。 But when I'm already in the first modal & I want to go into the second project (modal#2), he appears but my scroll is locked and my body lost his "modal-open" class.但是当我已经在第一个模态中并且我想进入第二个项目(模态#2)时,他出现了,但我的滚动被锁定并且我的身体失去了他的“模态开放”课程。

I think data-dismiss="modal" clear everything.我认为 data-dismiss="modal" 清除了一切。 But when I add manually (with inspector) the class "modal-open" on my body when the second modal is opened, everything works.但是,当我在打开第二个模态时在我的身体上手动(使用检查器)添加类“modal-open”时,一切正常。

So I tried to fix this with a lot a solution from forum post & nothing really works.因此,我尝试通过论坛帖子中的很多解决方案来解决此问题,但没有任何效果。

I think I had to look for this type of snippets for adding the class on the body automatically, something like that :我想我必须寻找这种类型的片段来自动在正文上添加类,就像这样:

    $(document)
.on('show.bs.modal', '.modal', function () { 
    $(document.body).addClass('modal-open') 
})
.on('hidden.bs.modal', '.modal', function () { 
    $(document.body).removeClass('modal-open') 
})

But actually, everything failed, obviously!但实际上,一切都失败了,很明显!

How can I fix this?我怎样才能解决这个问题?

If you want to take a look to my online test version: http://bg-portfolio.com/bg_test/index.php , scroll and click on the first project "Gamers Assembly 2017", then scroll and click on "Projet suivant" (next project).如果你想看看我的在线测试版本: http ://bg-portfolio.com/bg_test/index.php,滚动并点击第一个项目“Gamers Assembly 2017”,然后滚动并点击“Projet suivant” "(下一个项目)。 The bug will appear!错误会出现!

PS: I'm working with Bootstrap v3.3.7 (and when I paste news files, everything is broken, so for this website, I just want to stay under this version for now) PS:我正在使用 Bootstrap v3.3.7 (当我粘贴新闻文件时,一切都坏了,所以对于这个网站,我现在只想留在这个版本下)

Maybe could be more convenient, change a little bit the way as you show/hide your modals for each next/previous portfolio item.也许会更方便,在您显示/隐藏每个下一个/上一个投资组合项目的模式时稍微改变一下方式。

<button type="button" class="close2" data-actual="#Modal_GA" data-target="#Modal_Animations">
   <span aria-hidden="true">&gt;</span><p class="TxtClose">PROJET SUIVANT</p>
</button>

Then you can use only 1 simple jquery function:然后您只能使用 1 个简单的 jquery 函数:

  $('.close2').click(function() {
    var actualModal = $(this).attr('data-actual');
    var newModal = $(this).attr('data-target');

    $(actualModal).modal('hide');
    $(newModal).modal('show');
  }); 

Note that you don't need any more create a script for each portfolio item, in this way you will save time load and resource for the JS in the browser.请注意,您不再需要为每个投资组合项目创建脚本,这样您将节省浏览器中 JS 的时间加载和资源。

You've added the event listeners twice:您已经添加了两次事件侦听器:

When I view source I can see the following on lines 583 and 1046:当我查看源代码时,我可以在第 583 和 1046 行看到以下内容:

$(document)
.on('show.bs.modal', '.modal', function () { 
    $(document.body).addClass('modal-open') 
})
.on('hidden.bs.modal', '.modal', function () { 
    $(document.body).removeClass('modal-open') 
})

I quickly removed one of the event listeners in Chromes Inspect Tool.我迅速删除了 Chromes Inspect Tool 中的一个事件监听器。

Right click the button in the modal you mentioned > Inspect > Event Listeners > click > then delete one of the div#Modal_GA,modal.fade.in event listeners.右键单击您提到的模式中的按钮>检查>事件侦听器>单击> 然后删除其中一个div#Modal_GA,modal.fade.in事件侦听器。 The next modal now scrolled correctly for me.下一个模态现在为我正确滚动。

I assume, therefore, that by removing one of the two duplicated blocks, you will remove this second listener, which is basically firing twice and messing up your second modal...因此,我假设通过删除两个重复块中的一个,您将删除第二个侦听器,它基本上会触发两次并弄乱您的第二个模态......

<h1>Bootstrap 4x multiple modals in one page</h1>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">Large modal</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg1">Large modal1</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg2">Large modal2</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg3">Large modal3</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg4">Large modal4</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg5">Large modal5</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg6">Large modal6</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg7">Large modal7</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg8">Large modal8</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg9">Large modal9</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg10">Large modal10</button>


<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>
<div class="modal fade bd-example-modal-lg1" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel1" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal1</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>
<div class="modal fade bd-example-modal-lg2" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel1" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal2</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>
<div class="modal fade bd-example-modal-lg3" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel1"
    aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal3</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>
<div class="modal fade bd-example-modal-lg4" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel1"
    aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal4</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>
<div class="modal fade bd-example-modal-lg5" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel1"
    aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal5</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>
<div class="modal fade bd-example-modal-lg6" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel1"
    aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal6</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>
<div class="modal fade bd-example-modal-lg7" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel1"
    aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal7</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>
<div class="modal fade bd-example-modal-lg8" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel1"
    aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal8</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>
<div class="modal fade bd-example-modal-lg9" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel1"
    aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal9</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>
<div class="modal fade bd-example-modal-lg10" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel1"
    aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Large modal10</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>

The modal-open class gets removed after the modal has finished animating closed, but added by the other modal just as it opens. modal-open类在模态完成动画关闭后被删除,但在打开时由另一个模态添加。 Unfortunately these animations overlap.不幸的是,这些动画重叠。 This means that the modal-open class is added by the opening modal and then quickly removed by the closing modal (as I understand it).这意味着modal-open类由打开模态添加,然后由关闭模态快速删除(据我了解)。

Hooking into the late events of the modal (specifically shown.bs.modal , which is fired when the opening animation ends) should help you here: https://getbootstrap.com/docs/4.0/components/modal/#events挂钩模式的后期事件(特别是显示的shown.bs.modal ,在打开动画结束时触发)应该在这里帮助你: https ://getbootstrap.com/docs/4.0/components/modal/#events

$( '#modal1' ).on( 'shown.bs.modal', function(){
    document.body.classList.add( 'modal-open' );
});
$( '#modal2' ).on( 'shown.bs.modal', function(){
    document.body.classList.add( 'modal-open' );
});

Tested and working over here在这里测试和工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM