Skip to content

Instantly share code, notes, and snippets.

@haliphax
Created April 13, 2013 18:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haliphax/5379454 to your computer and use it in GitHub Desktop.
Save haliphax/5379454 to your computer and use it in GitHub Desktop.
Auto-scrolling extender for knockout.js observableArray objects
ko.extenders.scrollFollow = function (target, selector) {
target.subscribe(function (newval) {
var el = document.querySelector(selector);
// the scroll bar is all the way down, so we know they want to follow the text
if (el.scrollTop == el.scrollHeight - el.clientHeight) {
// have to push our code outside of this thread since the text hasn't updated yet
setTimeout(function () { el.scrollTop = el.scrollHeight - el.clientHeight; }, 0);
}
});
return target;
};
var viewModel = {
someArray: ko.observableArray().extend({ scrollFollow: '#some_element' })
};
ko.applyBindings(viewModel);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment