mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-10-31 23:53:00 +00:00
A bunch more source file reorganisations
Part one
This commit is contained in:
59
core/modules/utils/dom/scroller.js
Normal file
59
core/modules/utils/dom/scroller.js
Normal file
@@ -0,0 +1,59 @@
|
||||
/*\
|
||||
title: $:/core/modules/scroller.js
|
||||
type: application/javascript
|
||||
module-type: utils
|
||||
|
||||
Plugin that creates a $tw.utils.Scroller object prototype that manages scrolling in the browser
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
var slowInSlowOut = function(t) {
|
||||
return (1 - ((Math.cos(t * Math.PI) + 1) / 2));
|
||||
};
|
||||
|
||||
/*
|
||||
Creates a Scroller object
|
||||
*/
|
||||
var Scroller = function() {
|
||||
};
|
||||
|
||||
Scroller.prototype.cancel = function() {
|
||||
if(this.timerId) {
|
||||
window.clearInterval(this.timerId);
|
||||
this.timerId = null;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Smoothly scroll an element back into view if needed
|
||||
*/
|
||||
Scroller.prototype.scrollIntoView = function(element) {
|
||||
var scrollPosition = $tw.utils.getScrollPosition();
|
||||
this.cancel();
|
||||
this.startTime = new Date();
|
||||
this.startX = scrollPosition.x;
|
||||
this.startY = scrollPosition.y;
|
||||
this.endX = element.offsetLeft;
|
||||
this.endY = element.offsetTop;
|
||||
if((this.endX < this.startX) || (this.endX > (this.startX + window.innerWidth)) || (this.endY < this.startY) || (this.endY > (this.startY + window.innerHeight))) {
|
||||
var self = this;
|
||||
this.timerId = window.setInterval(function() {
|
||||
var t = ((new Date()) - self.startTime) / $tw.config.preferences.animationDuration;
|
||||
if(t >= 1) {
|
||||
self.cancel();
|
||||
t = 1;
|
||||
}
|
||||
t = slowInSlowOut(t);
|
||||
window.scrollTo(self.startX + (self.endX - self.startX) * t,self.startY + (self.endY - self.startY) * t);
|
||||
}, 10);
|
||||
}
|
||||
};
|
||||
|
||||
exports.Scroller = Scroller;
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user