Skip to content

Instantly share code, notes, and snippets.

@witmin
Last active August 28, 2019 09:25
Show Gist options
  • Save witmin/07ecc9cc26898ca319d582a09b932766 to your computer and use it in GitHub Desktop.
Save witmin/07ecc9cc26898ca319d582a09b932766 to your computer and use it in GitHub Desktop.
Pure JavaScript scroll to stick function for vue.js and CSS styles
<html>
<head>
.editor-main-menu.sticky {
position: fixed;
top: 0;
width: 100%;
z-index: 10;
}
.editor-main-menu.sticky + .sibling-element {
padding-top: 3.5rem;
}
</head>
<body>
<nav role="navigation" class="editor-main-menu" id="toolbar">
...
</nav>
</body>
<script>
export default {
mounted() {
// Make editor toolbar scroll to fix top
window.addEventListener('scroll', function () {
stickyEditorToolbar();
});
const toolbar = document.getElementById("toolbar");
const sticky = toolbar.offsetTop;
function stickyEditorToolbar() {
if (window.pageYOffset >= sticky) {
toolbar.classList.add("sticky")
} else {
toolbar.classList.remove("sticky");
}
}
}
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment