gpt4 book ai didi

jquery - 我是否添加了一堆不必要的事件处理程序?

转载 作者:行者123 更新时间:2023-12-01 05:04:23 26 4
gpt4 key购买 nike

我怀疑我在这里使用的事件处理程序可能是错误的。有人可以指出我正确/更好的方法吗?

基本上我正在监视 window.resize 事件。如果窗口小于屏幕上的元素,我将绑定(bind)到滚动事件。我的问题是调整大小事件不断抛出。我认为这意味着我不断重新绑定(bind)到滚动事件。这看起来很糟糕。有没有更好的方法来做到这一点?

我可以使用一个变量来跟踪它是否已经绑定(bind)......但这对我来说似乎很笨拙。

    //when window is resized check whether the sidebar still fits on screen
$(window).resize(checkIt);

function checkIt() {
botOfSidebar = $(obj).height() + topOfSidebar;
if (botOfSidebar < $(window).height()) {
//discard event handler
$(window).unbind("scroll", dynamicallyAdjustIt);
fixIt(); //fix it in place
}
else {
console.log("dynamically adjust it");
$(window).scroll(dynamicallyAdjustIt);
}
}

最佳答案

为什么不简单地绑定(bind)滚动事件一次,然后当大小低于阈值时,更新变量以启用它?

又名

$(window).resize(function() { });
$(window).scroll(function()
{
if( /* Check to see if window is greater than some set size */ )
return;

// Do Logic
});

关于jquery - 我是否添加了一堆不必要的事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7168198/

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