【问题标题】:Modal with link to same index具有链接到相同索引的模式
【发布时间】:2021-12-26 14:47:13
【问题描述】:

我创建了一个模式,其中我放置了一个href 链接,指向位于同一index.html 末尾的联系人部分。

当我单击 contact me 按钮时模式消失并且我没有向下滚动到联系人部分(我认为关闭模式后有某种内置向下滚动会破坏我自己的 href 链接)。

单击按钮后我尝试使用javascript方法,但也出现问题。 当使用window.location.reload 然后scrollintoview#kontakt 页面重新加载很好但scrollintoview 永远不会发生。

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModalToggle">
  Launch demo modal
</button>



<div class="modal fade" id="exampleModalToggle" aria-hidden="true" aria-labelledby="exampleModalToggleLabel" tabindex="-1">
            <div class="modal-dialog modal-dialog-centered" style="width:50%;margin:auto">
              <div class="modal-content">
                <div class="modal-body">                
                  <img src="https://www.apacara.com/media/images/orange.jpg" class="d-block w-100"                                              style="width:100%;border-radius:4px;margin:auto">
                </div>
              </div>
              <div class="modal-footer" style="text-align: center;">
                  <button class="btn btn-primary" style="float:left" data-bs-target="#exampleModalToggle6" data-                             bs-toggle="modal" data-bs-dismiss="modal">Next</button>
                  <a class="btn-grad" href="#kontakt" style="cursor:pointer;font-weight:500!important" data-bs-                        toggle="modal" data-bs-dismiss="modal">Contact me</a>
                  <button class="btn btn-primary" style="float:right" data-bs-target="#exampleModalToggle2" data-                           bs-toggle="modal" data-bs-dismiss="modal">Previous</button>
              </div>
            </div>
        </div>

<div id="kontakt" style="margin-top:500px">Helo helo helo helo</div>

【问题讨论】:

    标签: html hyperlink modal-dialog


    【解决方案1】:

    这与事件监听器的工作方式有关;它只会触发一次,即关闭模式。

    我所做的只是一个 hack,添加了一个超时,因此关闭模式和滚动查看不会同时触发

    const myModal = document.getElementById('exampleModalToggle');
    
    myModal.addEventListener('hidden.bs.modal', function (event) {
    setTimeout(function(){ 
       el = document.querySelector("#kontakt")
       el.scrollIntoView({behavior: 'smooth'}); 
       }, 10);
        
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModalToggle">
      Launch demo modal
    </button>
    
    
    
    <div class="modal fade" id="exampleModalToggle" aria-hidden="true" aria-labelledby="exampleModalToggleLabel" tabindex="-1">
                <div class="modal-dialog modal-dialog-centered" style="width:50%;margin:auto">
                  <div class="modal-content">
                    <div class="modal-body">                
                      <img src="https://www.apacara.com/media/images/orange.jpg" class="d-block w-100"                                              style="width:100%;border-radius:4px;margin:auto">
                    </div>
                  </div>
                  <div class="modal-footer" style="text-align: center;">
                      <button class="btn btn-primary" style="float:left" data-bs-target="#exampleModalToggle6" data-                             bs-toggle="modal" data-bs-dismiss="modal">Next</button>
                      <a class="btn-grad" href="#kontakt" style="cursor:pointer;font-weight:500!important" data-bs-                        toggle="modal" data-bs-dismiss="modal">Contact me</a>
                      <button class="btn btn-primary" style="float:right" data-bs-target="#exampleModalToggle2" data-                           bs-toggle="modal" data-bs-dismiss="modal">Previous</button>
                  </div>
                </div>
            </div>
    
    <div id="kontakt" style="margin-top:500px">Helo helo helo helo</div>

    【讨论】:

    • 它可以很好地向下滚动,但无法点击的深色背景仍然存在。 &lt;div class="modal-backdrop show"&gt;&lt;/div&gt; 这个类保留在正文顶部并阻止内容。
    • 我无法复制问题,您可以尝试将超时时间增加到 1000 毫秒,然后重试
    • 我认为超时不是问题,因为页面没有重新加载。关键是,当模式打开时,所有背景都变得不可滚动并变暗。当您关闭模式时,滚动和在页面上移动的能力应该会恢复并且深色背景消失。然而它并没有发生,只有滚动的能力回来了,但黑暗的背景和无法点击仍然出现。
    • 是否可以在不知道其 id 的情况下添加到 javascript 以删除特定的 div&lt;div class="modal-backdrop show"&gt;&lt;/div&gt; 确切地说。
    • 谢谢你的作品。最后一件事,如果我有多个模态怎么办?我应该只是复制和粘贴新的听众还是把所有的东西都放在一起?
    猜你喜欢
    • 2014-09-20
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    相关资源
    最近更新 更多