jquery:检查 window 是否已经滚动一次并做一些事情

jquery: check if window has been scrolled once and do something

由于 cookie 法的新实施,我必须征求一些 cookie 的同意。其中一种方法,是等待用户滚动页面一次,这在法律上意味着是一种默示同意。

我试过:

jQuery(window).one('scroll', function(){
  $(this).data('scrolled', true);
});

if(jQuery(window).data('scrolled')) {
    console.log( 'ok' );
    //place cookies
} else {
    console.log( 'no' );
}

在通过以下方式调用的脚本 ( http://www.primebox.co.uk/projects/jquery-cookiebar/ ) 中:

$(document).ready(function(){
                $.cookieBar({
                }); 
            });

然而,console.log 总是告诉我 "no",无论页面是否滚动。

有什么提示吗? 提前致谢

如果我这样更改,您的代码可以正常工作:

<script>
    jQuery(window).one('scroll', function()
    {
        $(this).data('scrolled', true);
        if(jQuery(window).data('scrolled'))
        {
            console.log('ok');
            //place cookies
        }
        else
        {
            console.log('no');
        }
    });
</script>

但是我无法弄清楚 $.cookieBar 在做什么?

  1. 检查滚动条并做一些你想做的事

    window.onscroll = 函数 (e) {
    // 当 window 滚动时调用。
    }

This will call every time when user scroll if you want to call it only once then do some trict

  1. 创建隐藏字段

  2. 现在在滚动方法调用时将此隐藏字段的值设置为 true

    window.onscroll = 函数 (e) { 如果 (document.getElementById("ScrollOnceCheck").value == "false") { document.getElementById("ScrollOnceCheck").value = "true"; //更新cookie } }