【问题标题】:Show popup message once only仅显示一次弹出消息
【发布时间】:2018-03-21 17:29:32
【问题描述】:

我有一个弹出消息,每次用户访问我的网页时都会显示。这很烦人,我只想显示一次弹出消息。该怎么做?

<script>
    $(document).ready(function () {
        $("#popup").hide().fadeIn(1000);
        //close the POPUP if the button with id="close" is clicked
        $("#close").on("click", function (e) {
            e.preventDefault();
            $("#popup").fadeOut(1000);
        });
    });
</script>
<!-- Pop up message -->
<link href="myhome/popup.css" rel="stylesheet" type="text/css"/>
<div class="body-popup" style="height: 100%; width: 100%; background-color: black">
    <div id="popup">
        <div id="close" class="alert alert-info alert-dismissable">
            <button type="button" class="pull-right close" data-dismiss="alert" aria-hidden="true">&times;</button>
            CLOSE
            <img src="/myhome/assets/img/prom.jpg" alt="popup" class="image-pop"/>
        </div>
    </div>
</div>
 

【问题讨论】:

    标签: javascript html laravel


    【解决方案1】:

    他们看到后,将其保存在 localStorage 中。

    $(document).ready(function () {
        const popup = $("#popup");
        popup.hide()
        if (localStorage.seenPopup) return;
        popup.fadeIn(1000);
        //close the POPUP if the button with id="close" is clicked
        $("#close").on("click", function (e) {
            e.preventDefault();
            $("#popup").fadeOut(1000);
            localStorage.seenPopup = 'true'; // value doesn't matter as long as it's not falsey
        });
    });
    

    【讨论】:

    • 为什么我只是发现'localstorage'?我现在想知道这是否比使用 cookie 更简单。
    • @CertainPerformance 感谢您的回答,我添加了您的代码但没有更改。
    • @Muharrem Smakiqi 固定,意识到无论如何你都想打电话给hide()
    • @CertainPerformance 我只想向任何用户显示一次,如果他单击关闭,则不应再为该用户显示弹出窗口。
    • @Muharrem Smakiqi 这就是代码的作用。 (不过,如果可以的话,更改 CSS 会更优雅,这样默认情况下不显示弹出窗口,而不是每次加载页面时最初调用 hide()
    【解决方案2】:

    您可以像这样编写和检查 cookie:

    ...
    const showPopup = $.cookie('noPopup');
    if (!noPopup) {
       $("#popup").hide().fadeIn(1000);
       $("#close").on("click", function (e) {
            e.preventDefault();
            $("#popup").fadeOut(1000);
            $.cookie('noPopup', true);
        });
    }
    ....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多