1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-29 15:30:47 +00:00

Refactor scroller so that the scrollIntoView() method is a bit more usable

Passing an event instead of a DOM node was silly.
This commit is contained in:
Jeremy Ruston 2013-07-08 15:15:53 +01:00
parent 09dbf26ce1
commit 53ead15273

View File

@ -45,7 +45,7 @@ Handle an event
*/ */
PageScroller.prototype.handleEvent = function(event) { PageScroller.prototype.handleEvent = function(event) {
if(event.type === "tw-scroll") { if(event.type === "tw-scroll") {
return this.scrollIntoView(event); return this.scrollIntoView(event.target);
} }
return true; return true;
}; };
@ -53,20 +53,19 @@ PageScroller.prototype.handleEvent = function(event) {
/* /*
Handle a scroll event hitting the page document Handle a scroll event hitting the page document
*/ */
PageScroller.prototype.scrollIntoView = function(event) { PageScroller.prototype.scrollIntoView = function(element) {
// Get the offset bounds of the element // Get the offset bounds of the element
var domNode = event.target, var bounds = {
bounds = { left: element.offsetLeft,
left: domNode.offsetLeft, top: element.offsetTop,
top: domNode.offsetTop, width: element.offsetWidth,
width: domNode.offsetWidth, height: element.offsetHeight
height: domNode.offsetHeight
}; };
// Walk up the tree adjusting the offset bounds by each offsetParent // Walk up the tree adjusting the offset bounds by each offsetParent
while(domNode.offsetParent) { while(element.offsetParent) {
domNode = domNode.offsetParent; element = element.offsetParent;
bounds.left += domNode.offsetLeft; bounds.left += element.offsetLeft;
bounds.top += domNode.offsetTop; bounds.top += element.offsetTop;
} }
// Now get ready to scroll the body // Now get ready to scroll the body
this.cancelScroll(); this.cancelScroll();