0

私には2つの機能があります:

1) ページロード時にボックスが非表示になります。ユーザーがページを下にスクロールすると、ボックスがスライドインします。ユーザーがボックスを閉じることを選択すると、ボックスが非表示になります。

2) 彼らがボックスを閉じるとき、このボックスは他のページに移動するときに閉じたままにする必要があります (私はこれを行うために Cookie を使用しています)。

2 つの機能を一緒に実装しようとすると、問題が発生し、互いに競合するようです。クッキー内にスクロール機能を配置すると、ボックスが完全に非表示になるため、誰かが私にガイダンスを与えることができますか?

コードは次のとおりです (すべてのコードは個別に動作します)。

スクロール コード:

jQuery(window).scroll(function() {
        if (jQuery(this).scrollTop() > 200) {
            jQuery('#test').stop().animate({ left: '0' });
        } else {
            jQuery('#test').stop().animate({ left: '-25%' });
        }
    });

クッキーコード

$(document).ready(function() {

  // If the 'hide cookie is not set we show the message
  if (!readCookie('hide')) {
    $('#test').show();
  }
  // Add the event that closes the popup and sets the cookie that tells us to
  // not show it again until one day has passed.
  $('#close').click(function() {
    $('#test').hide();
    createCookie('hide', true, 1)
    return false;
  });

});

// ---
// And some generic cookie logic
// ---
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}
});

html ダミーテキスト

<div id="test">poup with stuff in it
    <a href="JavaScript:void(0)" id="close">close</a>
</div>

私はこれの初心者ユーザーなので、2つをまとめただけの作業コードがあることを知っています。助けがあれば感謝します。

4

1 に答える 1