How to show DIV on first visit and not any visit after until browser is closed

前端 未结 3 1264
天涯浪人
天涯浪人 2021-01-24 15:57

I have the following script which slides in/slide out a DIV from the right:

// jQuery 1.9 > Toggle Event dep 1.8
$(\'#slideClick\').click(function () {
    va         


        
3条回答
  •  终归单人心
    2021-01-24 16:58

    UPDATED ANSWER PER COMMENTS

    If you know your browsers support it, you can use sessionStorage instead of the data element. Also to make it more DRY I have added additional functions. You would use it something like this:

    $(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');
      }
    });
    

提交回复
热议问题