Skip to content

Instantly share code, notes, and snippets.

@begedin
Created May 8, 2014 06:12
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 begedin/17d61ad8375d4b699e4c to your computer and use it in GitHub Desktop.
Save begedin/17d61ad8375d4b699e4c to your computer and use it in GitHub Desktop.
ScrollTo binding for knockout
// usage: data-bind="scrollTo: booleanObservable"
// will scroll to this item if observable is true
// made by eselk of stack excahnge: http://stackoverflow.com/users/1042232/eselk
// found in thread: http://stackoverflow.com/questions/10126812/knockout-js-get-dom-object-associated-with-data
ko.bindingHandlers.scrollTo = {
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
console.log(value);
if (value) {
var scrollParent = $(element).closest("div");
var newTop = $(element).position().top + scrollParent.scrollTop();
scrollParent.scrollTop(newTop);
}
}
};
@sohel3csedu
Copy link

how can i bind it from html, could u pls give some example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment