简体   繁体   中英

JavaScript TypeError: Cannot read property 'style' of null for scroll

I need to fix this but I dont know how to do it. I think it is easy but I am not good at js. But this codes work well.

 var prevScrollpos = window.pageYOffset; window.onscroll = function () { var currentScrollPos = window.pageYOffset; if (prevScrollpos > currentScrollPos) { document.getElementById("w-sticky-header").style.top = "0"; } else { document.getElementById("w-sticky-header").style.top = "-50px"; } prevScrollpos = currentScrollPos; }

This is my sticky header js code. But it results in an alert as seen in the picture:错误

Well technically it means code could not find element with given id. Please check your html that it does have this element with id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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