I'm experimenting with something new in Javascript. I would like to change the background color of an item when the scrollY
position has reached a specific value; 80 in this case. However, it's not working. Is there something wrong with the logic?
window.addEventListener("scroll", function (event){
var scroll = this.scrollY;
if scroll.value === '80' {
document.getElementById="Gegevens".style.background-color="lightblue" } else {}
})
There are a number of errors in your code:
if
conditionscroll.value
getElementById
needs brackets not an equals sign background-color
event
as a parameter when using this
(not an error but interesting either way)This is how it should be:
window.addEventListener("scroll", function(event) {
const scroll = this.scrollY;
if (scroll >= 80)
document.getElementById("Gegevens").style.backgroundColor="lightblue"
})
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.