如何在第一次访问时显示DIV,而不是在浏览器关闭之后的任何访问

我有以下脚本从右侧滑入/滑出DIV:

// jQuery 1.9 > Toggle Event dep 1.8 $('#slideClick').click(function () { var it = $(this).data('it') || 1; switch (it) { case 1: $(this).parent().animate({ right: '-290px' }, { queue: false, duration: 500 }); $("#imgArrow").attr("src", "../theImages/arrow_expand.png"); break; case 2: $(this).parent().animate({ right: '-0px' }, { queue: false, duration: 500 }); $("#imgArrow").attr("src", "http://sofzh.miximages.com/jquery/how-to-show-div-on-first-visit-and-not-any-visit-after-until-browser-is-closed"); break; } it++; if (it > 2) it = 1; $(this).data('it', it); }); 

MasterPage中与上述脚本对应的HTML代码为:

 
MESSAGES

每当用户访问该页面时,滑出是真的,这迫使用户单击滑动单击DIV以关闭它,这可能对用户不友好。

我如何使用cookie或会话,以便发生以下情况:

用户第一次访问时,一旦他们点击slideclick DIV进行隐藏,就会将DIV显示在滑出位置。 在浏览器关闭之前,它将继续对用户隐藏,在这种情况下,一旦用户再次重新打开浏览器,DIV应处于滑出位置,直到用户单击slideclick DIV隐藏,等等。

滑出位置:

在此处输入图像描述

隐藏位置:

在此处输入图像描述

每条评论都有更新的答案

如果您知道您的浏览器支持它,则可以使用sessionStorage而不是数据元素。 另外为了使它更干,我增加了额外的function。 你会使用这样的东西:

 $(function() { function showContent(dir) { var pxVal = '0px', img='arrow-collapse'; if (dir === 'close') { pxVal = '-290px'; img = 'arrow-expand'; } $('#slideOut').animate({ right: pxVal }, { queue: false, duration: 500 }); $("#imgArrow").attr("src", "../theImages/"+img+".png"); } function showHideContent () { var currVal = sessionStorage.getItem('showSlideArea'); if (currVal === 'false') { showContent( 'close'); currVal='true'; } else { showContent( 'open'); currVal='false'; } sessionStorage.setItem('showSlideArea', currVal); } $('#slideClick').on('click', showHideContent); var currVal = sessionStorage.getItem('showSlideArea'); if (currVal === 'true') { showContent('close'); } }); 

通过jquery设置cookie。

检查这个答案: https : //stackoverflow.com/a/8213459/4016757

或者直接进入插件: https : //github.com/carhartl/jquery-cookie

在asp.net代码中

make变量名为ex。 requestNumber并将其存储在会话中

在每个请求中为+1分配一个值

Div“slideClick”使用requestNumber添加类,前面是slideClick.Attributes["class","request_"+requestNumber]

在css

slideClick {display:hidden;} slideClick.request_1 {display:block;}

更新1

你也可以用

Request.UrlReferrer

如果访问者来自网站而不是你的,或者为null,则指定要出现的类

点击网站内的任何链接后,UrlReferrer将成为您的网站,然后不分配类出现。

最后

如上所述,根据来自UrlReferrer,cookie或会话的值get,将类添加到想要显示/消失的div

更新2

如果你坚持你的范围,试试这个

 var it = sessionStorage.getItem('it') || 1; if(it > 1) { $('#slideClick').parent().animate({ right: '-290px' }, { queue: false, duration: 0 }); $("#imgArrow").attr("src", "../theImages/arrow_expand.png"); } //var it = $('#slideClick').data('it') || 1; alert(it); // jQuery 1.9 > Toggle Event dep 1.8 $('#slideClick').click(function () { alert(it); switch (it) { case 1: $(this).parent().animate({ right: '-290px' }, { queue: false, duration: 500 }); $("#imgArrow").attr("src", "../theImages/arrow_expand.png"); break; case 2: $(this).parent().animate({ right: '-0px' }, { queue: false, duration: 500 }); $("#imgArrow").attr("src", "../theImages/arrow_collapse.png"); break; } it++; if (it > 2) it = 1; sessionStorage.setItem('it', it); }); 

这里的想法是检查值onload不要等待点击。

问候