簡體   English   中英

關閉模態窗口后防止頁面滾動回到頂部

[英]prevent page from scrolling back to top after closing modal window

知道關閉模態窗口時如何防止頁面滾動回到頁面頂部嗎? 我需要它保持與充滿文本的頁面相同的位置,否則您最終會迷失原狀。 我有這個工作,但由於某些奇怪的原因不再起作用。 謝謝。

頁面調用一個PHP請求,該請求經過一個while循環並顯示句子,單擊該句子可顯示其他信息。 這顯示在模式窗口中。

while ($row = $result->fetch_assoc())
{ ?> 
<a  href="#" class="nojump" id="mymodal" onclick="showModal(this)"  
style="font-size:16px;"  title="<? echo  $row ['title']  ?> 
 by
 <? echo $row ['author'] ?>" ><? echo $row ['first_line'] ?> </a>


<script>
  function showModal(el) {
    jQuery("#myModalLabel").text(el.title);
    jQuery("#myModal").modal()
  }

</script>
<script>
  jQuery('a.nojump').click(function(e)
  {
    // Cancel the default action
    e.preventDefault();
  });
</script>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
   <div class="modal-dialog" role="document">
      <div class="modal-content">  
         <div class="modal-header">    
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
               <span aria-hidden="true">&times;</span>
            </button>
         </div>  
         <div class="modal-body">   
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>  
         </div>  
         <div class="modal-footer">    
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
         </div>
    </div>
  </div>
</div>

已更新:我更新了JSBin ,這是您想要的嗎?

HTML:

 <a  href="#myModal" class="nojump" style="font-size:16px;" title="title" data-toggle="modal">click me</a>

Javascript:

jQuery('a.nojump').click(function(e) {  
      e.preventDefault();
      console.log('do something here')
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM