在 php 开始时仅加载启动画面一次

Load splash screen only once at start in php

我已经使用 jquery 的淡出选项创建了启动画面。它工作正常,但问题是每次我单击转到下一页时屏幕都在加载。 我只在启动时需要启动画面。 我想我需要使用会话或其他东西,但我找不到解决方案。 我正在使用以下脚本。

 $(document).ready(function () {
 $("#splashscreen").click(function () {
    $("#splashscreen").fadeOut(2000); 
 });
 });

您可以设置cookie并在页面刷新时检查它。我建议你像这样使用库:

https://github.com/js-cookie/js-cookie

一种选择是在PHP中处理这个问题,但您也可以在 JS 中使用本地存储。 这是一个相关的主题:How to set session variable in jquery?

是的,您可以为此使用会话。在代码的第一行添加 <?php session_start();

现在,稍后在您的代码中,您可以:

if(!$_SESSION['splash'])
{
    $_SESSION['splash'] = true;

    //echo your splash code here
}

这应该有效:

$(document).ready(function () {
    if( $.cookie('splashscreen') == null ) { // Here you are checking if cookie is existing if not you are showing a splash screen and set a cookie
        $("#splashscreen").fadeIn();
        $.cookie("splashscreen", 1, { expires : 10 }); // cookie is valid for 10 days
    }
    $("#splashscreen").click(function () {
        $("#splashscreen").fadeOut(2000); 
    });
});