下面的函数在用户访问页面时显示一个通知工具栏。工具栏从窗口顶部向下滑动,将页面内容向下推。
这样还可以,但是访问的页面多了效果就变烦了。我想要完成的是:仅在用户第一次与网站交互时才向下滑动通知工具栏。首次显示通知后,如果用户刷新页面或访问其他站点页面,则立即显示通知工具栏,无下拉效果。
我可能应该为此使用 SessionStorage,只是不知道如何使用。
$(function(){
$('#notification_toolbar').slideDown();
});
最佳答案
既然您已经提到了 sessionStorage
,那么使用它来保存一个标志来告诉您 slideDown 是否已经发生是很容易的。然后您可以使用该标志来确保它不会再次发生。
里面你下滑代码:
// perform the slide down only if the slide down flag wasn't set
if(sessionStorage.getItem('hasSlideDown') !== 'yes') { // check if slide down flag was set
sessionStorage.setItem('hasSlideDown', 'yes'); // set the slide down flag
$('#notification_toolbar').slideDown(); // perform the slide down
}
如何 sessionStorage作品:
A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.
关于javascript - 每个 session 只运行一次 Jquery slideDown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33229536/