1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-05 15:32:55 +02:00

Move scrolling to Infinite, fix horz scroll

This commit is contained in:
Fred Chasen 2014-11-25 23:20:28 -05:00
parent e09f007e13
commit b1ebc754cd
9 changed files with 553 additions and 505 deletions

View file

@ -1,16 +1,19 @@
EPUBJS.Infinite = function(container, axis){
EPUBJS.Infinite = function(container, limit){
this.container = container;
this.windowHeight = window.innerHeight;
this.tick = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
this.scrolled = false;
this.ignore = false;
this.displaying = false;
this.offset = 500;
this.views = [];
this.axis = axis;
// this.children = container.children;
// this.renderer = renderer;
this.tolerance = 100;
this.prevScrollTop = 0;
this.prevScrollLeft = 0;
if(container) {
this.start();
}
// TODO: add rate limit if performance suffers
};
@ -49,22 +52,38 @@ EPUBJS.Infinite.prototype.backwards = function() {
EPUBJS.Infinite.prototype.check = function(){
if(this.scrolled && !this.displaying) {
if(this.scrolled && !this.ignore) {
scrollTop = this.container.scrollTop;
scrollLeft = this.container.scrollLeft;
if(this.axis === "vertical") {
this.checkTop();
} else {
this.checkLeft();
}
this.trigger("scroll", {
top: this.container.scrollTop,
left: this.container.scrollLeft
top: scrollTop,
left: scrollLeft
});
// For snap scrolling
if(scrollTop - this.prevScrollTop > this.tolerance) {
this.forwards();
}
if(scrollTop - this.prevScrollTop < -this.tolerance) {
this.backwards();
}
if(scrollLeft - this.prevScrollLeft > this.tolerance) {
this.forwards();
}
if(scrollLeft - this.prevScrollLeft < -this.tolerance) {
this.backwards();
}
this.prevScrollTop = scrollTop;
this.prevScrollLeft = scrollLeft;
this.scrolled = false;
} else {
this.displaying = false;
this.ignore = false;
this.scrolled = false;
}
@ -72,62 +91,10 @@ EPUBJS.Infinite.prototype.check = function(){
// setTimeout(this.check.bind(this), 100);
};
EPUBJS.Infinite.prototype.checkTop = function(){
var scrollTop = this.container.scrollTop;
var scrollHeight = this.container.scrollHeight;
var direction = scrollTop - this.prevScrollTop;
var height = this.container.getBoundingClientRect().height;
var up = scrollTop + this.offset > scrollHeight-height;
var down = scrollTop < this.offset;
// Add to bottom
if(up && direction > 0) {
this.forwards();
}
// Add to top
else if(down && direction < 0) {
this.backwards();
}
// console.log(scrollTop)
this.prevScrollTop = scrollTop;
return scrollTop;
};
EPUBJS.Infinite.prototype.checkLeft = function(){
var scrollLeft = this.container.scrollLeft;
var scrollWidth = this.container.scrollWidth;
var direction = scrollLeft - this.prevscrollLeft;
var width = this.container.getBoundingClientRect().width;
var right = scrollLeft + this.offset > scrollWidth-width;
var left = scrollLeft < this.offset;
// Add to bottom
if(right && direction > 0) {
this.forwards();
}
// Add to top
else if(left && direction < 0) {
this.backwards();
}
// console.log(scrollTop)
this.prevscrollLeft = scrollLeft;
return scrollLeft;
};
EPUBJS.Infinite.prototype.scrollBy = function(x, y, silent){
if(silent) {
this.displaying = true;
this.ignore = true;
}
this.container.scrollLeft += x;
this.container.scrollTop += y;
@ -138,7 +105,7 @@ EPUBJS.Infinite.prototype.scrollBy = function(x, y, silent){
EPUBJS.Infinite.prototype.scrollTo = function(x, y, silent){
if(silent) {
this.displaying = true;
this.ignore = true;
}
this.container.scrollLeft = x;
this.container.scrollTop = y;
@ -153,7 +120,6 @@ EPUBJS.Infinite.prototype.scrollTo = function(x, y, silent){
// };
this.check();
};
RSVP.EventTarget.mixin(EPUBJS.Infinite.prototype);