[英]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.