Last active
April 27, 2024 06:01
-
-
Save noeleo25/3190ad3542edf8717169f082fb1e639d to your computer and use it in GitHub Desktop.
Custom directive: sticky header after scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<!-- using custom directive v-sticky --> | |
<div class="w-100" v-sticky:top="{minWidth: 768, offset: 'vh'}"> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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