gpt4 book ai didi

jquery setInterval或滚动

转载 作者:行者123 更新时间:2023-12-03 22:30:00 24 4
gpt4 key购买 nike

我正在开发一个项目,我需要监听 scroll 事件..我想知道什么是更好的方法..

第一种方法

 function scroll() {
if ($(window).scrollTop() > 200) {
top.fadeIn();
} else {
top.fadeOut();
}
if (menuVisible) {
quickHideMenu();
}
}

第二种方法

      function scroll() {
didScroll = true;
}

setInterval(function() {
if ( didScroll ) {
didScroll = false;
if ($(window).scrollTop() > 200) {
top.fadeIn();
} else {
top.fadeOut();
}
if (menuVisible) {
quickHideMenu();
}
}
}, 250);

谢谢:)

最佳答案

都不是。我刚刚阅读了有关 JS/jQuery 模式的内容。有一个窗口滚动事件的示例:jQuery Window Scroll Pattern

var scrollTimeout;  // global for any pending scrollTimeout

$(window).scroll(function () {
if (scrollTimeout) {
// clear the timeout, if one is pending
clearTimeout(scrollTimeout);
scrollTimeout = null;
}
scrollTimeout = setTimeout(scrollHandler, 250);
});

scrollHandler = function () {
// Check your page position
if ($(window).scrollTop() > 200) {
top.fadeIn();
} else {
top.fadeOut();
}
if (menuVisible) {
quickHideMenu();
}
};

源自这里:Javascript Patterns

关于jquery setInterval或滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15591002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com