繁体   English   中英

如何在页面加载时弹出模式

[英]How to popup modal on page load

我正在学习如何在页面加载时弹出模式。 例如,当我单击 home.html 中链接到 comment.html 的按钮时,当 comment.html 加载然后模态弹出窗口时。 但是,如果我导航到 comment.html 而不单击按钮,则不会弹出模式。 仅当我单击按钮时才弹出模式。

主页.html

<!-- Another button that linked to comment without mod data-target -->
# I do not want Modal to popup in comment.html when I click on this button
<a href="{% url 'site:comment' %}">Comment</a>

<!-- Button trigger modal -->
# I want modal to popup in comment.html when I click on this button
<a href="{% url 'site:comment' %}" data-toggle="modal" data-target="#basicExampleModal">
Launch Modal in Comment
</a>

评论.html

<!-- Modal -->
<div class="modal fade" id="basicExampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
    ...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
$(document).ready(function(){  
  if(localStorage.getItem('popState') != 'shown'){
    $("#basicExampleModal").delay(2000).fadeIn();
    localStorage.setItem('popState','shown')
  }
   $('#basicExampleModal').modal('show');
    }); 

上面的代码将自动创建一个弹出窗口 Model。 希望你觉得它有用。

简而言之:

  • 在导航到评论页面之前创建一个本地存储数据项(例如项目名称为:showModalInCommentPage)
  • 在加载评论页面时检查本地存储数据项 (showModalInCommentPage) 是否存在
  • 如果存在加载模态并删除本地存储数据项(showModalInCommentPage)

在主页模板文件中

<!-- If modal should not be shown -->
<a href="{% url 'site:comment' %}">Comment</a>

<!-- If modal should be shown : create local storage item on clicking the a tag -->
<a href="{% url 'site:comment' %}" onclick="localStorage.setItem('showModalInCommentPage', '1')">Launch Modal in Comment</a>

在评论模板文件中

<script>
    $(document).ready( function() {
        var showmodal = localStorage.getItem('showModalInCommentPage');
        if (showmodal == '1') {
            $('#basicExampleModal').modal('show');
            localStorage.removeItem('showModalInCommentPage');
        }
    });
</script>

暂无
暂无

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

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