需要帮助在jQuery中设置cookie

时间:2011-10-21 22:46:58

标签: jquery cookies

很抱歉,如果这个问题听起来很愚蠢,但我只知道HTML / CSS,所以我想尽力解决问题。

当有人向我的页面滚动时,我正在使用jQuery创建向下滑动效果。

示例:http://buckinvestor.com/test/jquerytest.html

我认为这一切都包括在内,包括一个“关闭”按钮,但现在我必须弄清楚如何设置一个cookie,这样用户就不会在每一页上都停止下滑。我想将cookie设置为10天。

我读到我应该使用:

http://plugins.jquery.com/project/cookie

and then to write a cookie do $.cookie("test", 1);
to access the set cookie do $.cookie("test");

但我不知道我应该把这段代码放在哪里。有人可以告诉我我需要准确的代码和地点吗?而且,在设置像这样的cookie时,我应该注意什么?

谢谢StackOverflow众神!

你一直是一个救生员。

1 个答案:

答案 0 :(得分:1)

我想你想做这样的事情:

$(function() {
    var bar = $('#headerSlideContainer'),
        top = bar.css('top');
    if (!($.cookie("test"))) {
        $(window).bind("scroll", function() {
            if($(this).scrollTop() > 50) {
                bar.stop().animate({'top' : '0px'}, 500);
            } else {
                bar.stop().animate({'top' : top}, 500);
            }
        });
        $.cookie("test", 1);
    }
});