简体   繁体   English

让jQuery事件发生一次

[英]Make a jQuery event happen once

I'm making a wordpress template. 我正在制作一个wordpress模板。 Imagine I have a that contains a menu bar with 想象一下,我有一个包含菜单栏的菜单栏

$(document).ready(function() {
      $('#menu').hide();
      $('#menu').slideToggle("slow");
});

my menu slide down (it's a vertical menu), when the page loads... but, everytime I load a new static page in wordpress (pageid=1 etc), the page reload itself and so the event restart and the menu slide down again every time I press a link in the website! 我的菜单向下滑动(这是一个垂直菜单),当页面加载时......但是,每次我在wordpress中加载一个新的静态页面(pageid = 1等)时,页面会自动重新加载,因此事件重新启动,菜单向下滑动每次我按下网站上的链接再次!

How can I make the menu sliding animation happends one time? 如何让菜单滑动动画一次发生? maybe only when I hit the home button and not all the pages? 也许只有当我点击主页按钮而不是所有页面时?

set a session variable $_SESSION['opened']==1; 设置会话变量$_SESSION['opened']==1; only on homepage 只在主页上

in header where is that jquery code: 在标头中哪个是jquery代码:

<?php if(isset($_SESSION['opened']) && $_SESSION['opened'] == 1): ?>

your jquery code 你的jquery代码

<?php endif; ?>

In the rest of the pages, set that variable 0 在其余页面中,将该变量设置为0

You could make this event happen only in your site's homepage, like: 您可以仅在您网站的主页上发生此事件,例如:

<?php
if (is_home()):
?>
<style type="text/javascript">
$(document).ready(function() {
      $('#menu').hide();
      $('#menu').slideToggle("slow");
});
</style>
<?php endif; ?>

but if you want the user to see the animation even when he enters by any other page, you'll probably need some cookies ;P 但是如果你希望用户即使进入任何其他页面也能看到动画,你可能需要一些饼干; P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM