Skip to content

Instantly share code, notes, and snippets.

@noeleo25
Last active April 27, 2024 06:01
Show Gist options
  • Save noeleo25/3190ad3542edf8717169f082fb1e639d to your computer and use it in GitHub Desktop.
Save noeleo25/3190ad3542edf8717169f082fb1e639d to your computer and use it in GitHub Desktop.
Custom directive: sticky header after scroll
<template>
<!-- using custom directive v-sticky -->
<div class="w-100" v-sticky:top="{minWidth: 768, offset: 'vh'}">
</template>
Vue.directive('sticky',{
bind: function (el, binding) {
el.scrollEvent = function() {
var offset = binding.value.offset;
if(offset == 'vh')
offset = window.innerHeight;
if( window.innerWidth >= binding.value.minWidth){
if(binding.arg == 'top' && window.scrollY >= offset){
el.style.position = 'fixed';
el.classList.add("el-fixed");
}
else{
el.style.position = 'relative';
el.classList.remove("el-fixed");
}
}
}
document.addEventListener('scroll', el.scrollEvent);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment