onLoad函数不工作,如何使用BindListener修复它

onLoad function not working, how to fix it using BindListener?

本文关键字:BindListener 何使用 函数 工作 onLoad      更新时间:2024-05-02

所以现在我已经在Javascript中创建了一个onload函数。。。。但当我发现我不能在我正在开发的应用程序中使用任何onLoad javascript函数时,已经很晚了:(

<script type="text/javascript">
var sticky = document.querySelector('.sticky');
var origOffsetY = sticky.offsetTop;
function onScroll(e) {
window.scrollY >= origOffsetY ? sticky.classList.add('fixed') :
                              sticky.classList.remove('fixed');
}
document.addEventListener('scroll', onScroll);
</script>

有人建议我使用BindListeners,但我不知道它是如何工作的?有什么帮助吗?我也不能使用jQuery。。。。

var sticky = document.querySelector('.sticky');
var origOffsetY = sticky.offsetTop;
function onScroll(e) {
window.scrollY >= origOffsetY ? sticky.classList.add('fixed'):sticky.classList.remove('fixed');
}    
(function() {
  document.addEventListener('scroll', onScroll);
})();

你在用jQuery吗?