0

我有两个功能:

1)该框在页面加载时隐藏。当用户向下滚动页面这么多时,应该会滑入一个框。当他们选择关闭框时,框会隐藏。

2)当他们关闭盒子时,当导航到其他页面时,这个盒子应该保持关闭状态(我使用cookies来做到这一点),

试图同时实现这两个功能似乎是一个问题,并且相互冲突。有人可以给我一些指导,因为当将滚动功能放在 cookie 中时,它会完全隐藏框。

这是代码(所有代码单独工作):

滚动代码:

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

1

您是否使用 CSSdisplay: none;属性在初始显示时隐藏 div?我可以让你的代码像这个小提琴一样工作:http: //jsfiddle.net/t35cq/1/

CSS:

.main {
    height: 1000px;
}
.test {
    display: none;
    position: absolute;
    left: -100%;
    top: 150px;
    border: 1px solid black;
}

HTML:

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

    </div>
</div>

JavaScript:

// ---
// 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);
}

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

// Manually display or hide using code.
//createCookie('hide', true, 1);
eraseCookie('hide');

$(document).ready(function () {
    // If the 'hide cookie is not set we show the message
    if (!readCookie('hide')) {
        console.log('x');
        $('#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;
    });

});
于 2013-10-17T12:51:16.090 回答