jQuery:网页仅淡入一次

发布于 2024-09-13 16:21:12 字数 315 浏览 2 评论 0原文

我有一个简单的脚本可以淡入网站的主页,例如:

jQuery(document).ready(function(){    
  $('body.home').hide();
  $('body.home').fadeIn('slow');
});

但是,我希望该脚本只能在网站最初加载后运行一次。在随后的点击中,例如,当用户转到辅助页面然后返回主页时,我不希望主页再次淡入,它应该只是主页加载而不会淡出。

有没有办法只用 jQuery 代码来做到这一点,也许以某种方式只执行该函数一次,然后停止它执行?任何提示和代码片段将不胜感激!

I have a simple script to fade in the homepage of the website, something like:

jQuery(document).ready(function(){    
  $('body.home').hide();
  $('body.home').fadeIn('slow');
});

However, I would like the script to work only one time, once the website loads initially. On subsequent clicks when, say, a user goes to a secondary page and then returns to the homepage, I don't want the homepage to fade in again, it should be just homepage loading without fading.

Is there a way to do it with just jQuery code, maybe somehow execute the function only once and then stop it from executing? Any tips and code snippets will be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女 2024-09-20 16:21:12

你可以设置一个cookie。

$(function () {
  if ($.cookie("loaded") != "true") {
      $('body.home').hide().fadeIn('slow');
      $.cookie("loaded", "true");
  }
});

You could set a cookie.

$(function () {
  if ($.cookie("loaded") != "true") {
      $('body.home').hide().fadeIn('slow');
      $.cookie("loaded", "true");
  }
});
唔猫 2024-09-20 16:21:12

你可以潜入 php 的世界并检查是否存在会话,如果不存在..淡入。
如果您想沿着这条路线走下去,我可以发布一些代码

如果您不熟悉 php 和会话,请快速阅读:
http://www.tizag.com/phpT/phpsessions.php

you could dive into the world of php and check to see if a session exists, if there in not one.. fade in.
I could post some code if you wanted to go down this route

A quick read if your new to php and sessions:
http://www.tizag.com/phpT/phpsessions.php

×眷恋的温暖 2024-09-20 16:21:12

如果您仅通过 AJAX 调用加载网站的“其他部分”,则可以在全局 javascript 命名空间中存储一个值,该值仅指示您已经执行了该操作。

否则,您将进行完整的页面刷新(重定向到另一个站点),没有干净的 JavaScript 解决方案。在这种情况下,您需要存储 cookie、使用本地存储 或使服务器脚本发挥作用。

If you're loading "other parts" of your site only via AJAX calls, you can store a value in global javascript namespace which just indicates that you already executed that action.

Otherwise, you're doing a complete page refresh (redirect to another site) there is no clean javascript solution. In that case you need to store a cookie, make usage of the local storage or bring your server script into play.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文