简体   繁体   English

滚动仅触发一次

[英]onscroll only fires once

I have a javascript file I am using to align a single absolute div to always be inline with the horizontal scroll position. 我有一个javascript文件,用于对齐单个绝对div,使其始终与水平滚动位置对齐。

I can't figure out what's not working with my code - onscroll seems to fire once and then never fire again in both internet explorer and firefox. 我无法弄清楚什么无法使用我的代码-滚动似乎会触发一次,然后再也不会在Internet Explorer和firefox中触发。

Anyone have any ideas? 有人有想法么? I've been looking this up for a bit and I can't figured out the error. 我一直在找这个,但我找不到错误。 (I have tried both onscroll += and onscroll =, neither work). (我已经尝试过onscroll + =和onscroll =,但都没有效果)。 That 39 offset is just to make sure the script fired until I get it to work. 那39个偏移量只是为了确保脚本能够触发,直到我开始工作为止。

 window.onload=scrollSet; function scrollSet(){ window.onscroll += KeepLeftAtScroll(); } function KeepLeftAtScroll(){ var ele=document.getElementById("topAboveMenuMargin"); if (ele != null){ ele.style.left = 39+ document.documentElement.scrollLeft; } } 

Fixed the issue. 解决了该问题。 Apparently javascript does not like seperating assignement and declaration, as 显然,javascript不喜欢分隔分配和声明,因为

 window.onscroll = function KeepLeftAtScroll(){ var ele=document.getElementById("topAboveMenuMargin"); if (ele != null) { ele.style.left = window.pageXOffset; } } 

works and does what I want it to. 工作,并做我想要的。 It didn't seem to like "window.onscroll = KLAS()" but was ok with "window.onscroll = function KLAS(){}". 它似乎不喜欢“ window.onscroll = KLAS()”,但可以使用“ window.onscroll = function KLAS(){}”。 I originally did the onload portion since I wasn't originally checking for null on my element and did it that way to get around that issue. 我最初进行了onload部分,因为我最初并未检查元素是否为null,并且以此方式解决了该问题。

Either way, it seems solved. 无论哪种方式,似乎都解决了。 Thanks for all the help all. 感谢所有的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM