const options = {
offset: -50
};
export default {
name: 'BarMenu',
data() {
return {
scrollingDirection: '',
}
},
computed: {
cssClasses() {
return {
hidden: this.scrollingDirection === 'down'
}
}
},
mounted() {
this.onScroll();
},
methods: {
scrollToIngredients() {
return this.$scrollTo(document.getElementById('ingredients-inside-content'), 600, options);
},
scrollToRecipes() {
return this.$scrollTo(document.getElementById('similar-recipes'), 600, options);
},
scrollToComments() {
return this.$scrollTo(document.getElementById('comments'), 600, options);
},
onScroll() {
let lastScrollTop = 0;
const handler = () => {
let st = window.pageYOffset || document.documentElement.scrollTop;
if (st > lastScrollTop) {
this.scrollingDirection = 'down';
} else {
this.scrollingDirection = 'up';
}
lastScrollTop = st <= 0 ? 0 : st;
};
window.removeEventListener("scroll", handler, false);
window.addEventListener("scroll", handler, false);
}
},
}
This line is not working, why? window.removeEventListener("scroll", handler, false);
I tried all approaches: arrow function, function declaration, etc. But that event listener is not removing at all.
handlerwon't be the samehandlerevery timescrollis run - therefore if your code is trying to remove the old handler and replace it, then it won't work - you could makehandlera method instead (make it an ordinary function though, not anarrow function- though, why you'd want to change the handler to the same code every time you run onScroll is a mystery