mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-12 12:59:57 +00:00
PageScroller: scroll in new windows (#3537)
* make pageScroller work in new windows * update getScrollPosition to work for new windows * Update dom.js
This commit is contained in:
parent
8ae62c90df
commit
55b5b6dd56
@ -82,11 +82,12 @@ Returns:
|
||||
y: vertical scroll position in pixels
|
||||
}
|
||||
*/
|
||||
exports.getScrollPosition = function() {
|
||||
if("scrollX" in window) {
|
||||
return {x: window.scrollX, y: window.scrollY};
|
||||
exports.getScrollPosition = function(srcWindow) {
|
||||
var scrollWindow = srcWindow || window;
|
||||
if("scrollX" in scrollWindow) {
|
||||
return {x: scrollWindow.scrollX, y: scrollWindow.scrollY};
|
||||
} else {
|
||||
return {x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop};
|
||||
return {x: scrollWindow.document.documentElement.scrollLeft, y: scrollWindow.document.documentElement.scrollTop};
|
||||
}
|
||||
};
|
||||
|
||||
@ -119,7 +120,7 @@ exports.resizeTextAreaToFit = function(domNode,minHeight) {
|
||||
Gets the bounding rectangle of an element in absolute page coordinates
|
||||
*/
|
||||
exports.getBoundingPageRect = function(element) {
|
||||
var scrollPos = $tw.utils.getScrollPosition(),
|
||||
var scrollPos = $tw.utils.getScrollPosition(element.ownerDocument.defaultView),
|
||||
clientRect = element.getBoundingClientRect();
|
||||
return {
|
||||
left: clientRect.left + scrollPos.x,
|
||||
|
@ -33,9 +33,9 @@ var PageScroller = function() {
|
||||
};
|
||||
};
|
||||
|
||||
PageScroller.prototype.cancelScroll = function() {
|
||||
PageScroller.prototype.cancelScroll = function(srcWindow) {
|
||||
if(this.idRequestFrame) {
|
||||
this.cancelAnimationFrame.call(window,this.idRequestFrame);
|
||||
this.cancelAnimationFrame.call(srcWindow,this.idRequestFrame);
|
||||
this.idRequestFrame = null;
|
||||
}
|
||||
};
|
||||
@ -55,12 +55,13 @@ Handle a scroll event hitting the page document
|
||||
*/
|
||||
PageScroller.prototype.scrollIntoView = function(element) {
|
||||
var self = this,
|
||||
duration = $tw.utils.getAnimationDuration();
|
||||
duration = $tw.utils.getAnimationDuration(),
|
||||
srcWindow = element.ownerDocument.defaultView;
|
||||
// Now get ready to scroll the body
|
||||
this.cancelScroll();
|
||||
this.cancelScroll(srcWindow);
|
||||
this.startTime = Date.now();
|
||||
// Get the height of any position:fixed toolbars
|
||||
var toolbar = document.querySelector(".tc-adjust-top-of-scroll"),
|
||||
var toolbar = srcWindow.document.querySelector(".tc-adjust-top-of-scroll"),
|
||||
offset = 0;
|
||||
if(toolbar) {
|
||||
offset = toolbar.offsetHeight;
|
||||
@ -68,7 +69,7 @@ PageScroller.prototype.scrollIntoView = function(element) {
|
||||
// Get the client bounds of the element and adjust by the scroll position
|
||||
var getBounds = function() {
|
||||
var clientBounds = element.getBoundingClientRect(),
|
||||
scrollPosition = $tw.utils.getScrollPosition();
|
||||
scrollPosition = $tw.utils.getScrollPosition(srcWindow);
|
||||
return {
|
||||
left: clientBounds.left + scrollPosition.x,
|
||||
top: clientBounds.top + scrollPosition.y - offset,
|
||||
@ -96,17 +97,17 @@ PageScroller.prototype.scrollIntoView = function(element) {
|
||||
t = ((Date.now()) - self.startTime) / duration;
|
||||
}
|
||||
if(t >= 1) {
|
||||
self.cancelScroll();
|
||||
self.cancelScroll(srcWindow);
|
||||
t = 1;
|
||||
}
|
||||
t = $tw.utils.slowInSlowOut(t);
|
||||
var scrollPosition = $tw.utils.getScrollPosition(),
|
||||
var scrollPosition = $tw.utils.getScrollPosition(srcWindow),
|
||||
bounds = getBounds(),
|
||||
endX = getEndPos(bounds.left,bounds.width,scrollPosition.x,window.innerWidth),
|
||||
endY = getEndPos(bounds.top,bounds.height,scrollPosition.y,window.innerHeight);
|
||||
window.scrollTo(scrollPosition.x + (endX - scrollPosition.x) * t,scrollPosition.y + (endY - scrollPosition.y) * t);
|
||||
endX = getEndPos(bounds.left,bounds.width,scrollPosition.x,srcWindow.innerWidth),
|
||||
endY = getEndPos(bounds.top,bounds.height,scrollPosition.y,srcWindow.innerHeight);
|
||||
srcWindow.scrollTo(scrollPosition.x + (endX - scrollPosition.x) * t,scrollPosition.y + (endY - scrollPosition.y) * t);
|
||||
if(t < 1) {
|
||||
self.idRequestFrame = self.requestAnimationFrame.call(window,drawFrame);
|
||||
self.idRequestFrame = self.requestAnimationFrame.call(srcWindow,drawFrame);
|
||||
}
|
||||
};
|
||||
drawFrame();
|
||||
|
Loading…
Reference in New Issue
Block a user