仅在Asp.Net Core 3 Web应用程序中显示一次模态

问题描述 投票:0回答:2

我更新了我工作的网站之一,我们的主页上有一个欢迎模式。我只希望该模式出现60天,并且只有某人第一次浏览该页面。 (很明显,每次您移至该页面时,它都会立即显示)

我可以在用户登录后添加声明,以控制该声明,但是对于未注册的基本用户该怎么做?

[模式是通过窗口onload javascript事件加载的。我认为应该在那里检查日期?

<script>

    $(window).on('load',function(){

        $('#modal-welcome').modal('show');

    });
    //make sure modal is in center of screen
    function alignModal() {
        var modalDialog = $(this).find(".modal-dialog");
        /* Applying the top margin on modal dialog to align it vertically center */
        modalDialog.css("margin-top", Math.max(0, ($(window).height() - modalDialog.height()) / 2));
    }
    // Align modal when it is displayed
    $(".modal").on("shown.bs.modal", alignModal);

    // Align modal when user resize the window
    $(window).on("resize", function () {
        $(".modal:visible").each(alignModal);
    });

</script>

这里是模态

<!-- Welcome Modal -->
<div id="modal-welcome" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-popout">
        <div class="modal-content">
            <div class="block block-themed block-transparent remove-margin-b">
                <div class="block-header bg-primary-dark">
                    <ul class="block-options">
                        <li>
                          <button data-dismiss="modal" type="button"><i class="si si-close"></i></button>
                        </li>
                    </ul>
                    <h3 class="block-title text-center">Welcome to our updated website!</h3>
                </div>
                <div class="block-content">
                    <partial name="_WelcomePartial" />                    
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-sm btn-default" type="button" data-dismiss="modal">Close</button>
                <button id="modal-welcome-agree" class="btn btn-sm btn-primary" type="button" data-dismiss="modal"><i class="fa fa-check"></i>Surf Away!</button>
            </div>
        </div>
    </div>
</div>
<!-- END Terms Modal -->
javascript bootstrap-modal asp.net-core-3.0
2个回答
1
投票

通过一些不同的术语进行搜索。现在有sessionStorage可以持续HTML 5中的整个浏览会话。这是要走的路。脚本很简单。

 <script>
    var today = new Date();
    //put whatever future date you want below
    var showUntil = new Date("2019-12-31");

    if (sessionStorage.getItem("shown") === null) {
        //show only until the end of the year
        if (today < showUntil) {
            $(window).on('load',function(){
                $('#modal-welcome').modal('show');
                sessionStorage.setItem("shown","true");
            });
        }
    }
 </script>

0
投票

您需要在页面上找到任何模式,并在每个事件周围添加“如果不可见”,则类似这样:

// assuming "modal" is a class that exists on every modal
if (!$(".modal").is(":visible")) {
  // event that makes a modal appear goes here
}

因此逻辑上讲,如果没有可见的模态,则使该可见。

© www.soinside.com 2019 - 2024. All rights reserved.