Skip to content

Instantly share code, notes, and snippets.

@Dosant
Created June 16, 2018 20:53
Show Gist options
  • Save Dosant/b2f777d6cb23080d9d17e181caaaf2b2 to your computer and use it in GitHub Desktop.
Save Dosant/b2f777d6cb23080d9d17e181caaaf2b2 to your computer and use it in GitHub Desktop.
Chat scroll
const scrollToBottom = (el, smooth) => {
el.scroll({
top: el.scrollHeight,
behavior: smooth ? 'smooth' : 'instant'
});
};
const vChatScroll = {
bind: (el, binding) => {
let scrolled = false;
el.addEventListener('scroll', e => {
scrolled = el.scrollTop + el.clientHeight + 1 < el.scrollHeight;
});
(new MutationObserver(e => {
let config = binding.value || {};
let pause = config.always === false && scrolled;
if (pause || e[e.length - 1].addedNodes.length != 1) return;
scrollToBottom(el, config.smooth);
})).observe(el, {childList: true});
},
inserted: scrollToBottom
};
export default vChatScroll;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment