You can use a Custom Directive and bind it to any component or element in your Vue instance.
So say you have a <div> and name your directive on-scroll you would update your div: <div on-scroll="handleScroll"> where handleScroll is the method in your Vue instance that is going to, well, handle the scroll event.
Directive:
Vue.directive('on-scroll', {
inserted: function (el, binding) {
let f = function (evt) {
if (binding.value(evt, el)) {
window.removeEventListener('scroll', f)
}
}
window.addEventListener('scroll', f)
}
})
Vue Instance:
new Vue({
el: '#el',
methods: {
handleScroll: (event, el) => {
if ( window.scrollY >= 300 ) {
el.classList.add('fixed');
} else {
el.classList.remove('fixed');
}
}
}
});