【问题标题】:jQuery: Run intro class only the first time you enter the website (or on a hard refresh)jQuery:仅在您第一次进入网站时(或在硬刷新时)运行介绍类
【发布时间】:2019-10-10 22:38:34
【问题描述】:
// Intro animation
setTimeout(function(){ 
    $('.intro').addClass("intro_hide")
  }, 3000);

我只想在您第一次进入网站时显示此 div(介绍)。当您转到另一个页面并返回主页时,这不应该显示。仅当您进行硬刷新或长时间返回网站时。

【问题讨论】:

标签: jquery


【解决方案1】:

你应该存储用户播放过介绍的地方,这个存储应该比变量更持久,所以你应该使用 cookie、localStorage 或 sessionStorage。 SessionStorage 符合您的要求,因为它会在窗口或选项卡关闭时重置其状态。

// Hide intro if has been played before
if (sessionStorage.getItem("introPlayed")) {
  $('.intro').hide()
} else {
  // Intro animation
  setTimeout(function() { 
    $('.intro').addClass("intro_hide");
    // Store information that user has played intro
    // In sessionStorage data is persisted only until the window or tab is closed
    sessionStorage.setItem("introPlayed", true);
  }, 3000);
}

【讨论】:

  • 这行得通!非常感谢你。你的解释也很清楚,我明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多