Created
May 8, 2014 06:12
-
-
Save begedin/17d61ad8375d4b699e4c to your computer and use it in GitHub Desktop.
ScrollTo binding for knockout
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
// 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); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how can i bind it from html, could u pls give some example?