![](/img/trans.png)
[英]jQuery, if window has scrolled X pixels do this, else if has scrolled XX pixels do something else
[英]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总是告诉我“否”。
有什么提示吗? 提前致谢
如果我这样更改,则您的代码可以正常工作:
<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在做什么?
检查滚动并做一些你想做的事
window.onscroll =函数(e){
//滚动窗口时调用。
}
如果用户只想滚动一次,则每次滚动时都会调用一次
做一个隐藏的领域
现在在滚动方法调用时将此隐藏字段的值设置为true
window.onscroll =函数(e){如果(document.getElementById(“ ScrollOnceCheck”)。value ==“ false”){document.getElementById(“ ScrollOnceCheck”)。value =“ true”; //更新cookie}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.