1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-03 14:59:18 +02:00

throttle check on scroll

This commit is contained in:
Fred Chasen 2015-05-17 23:59:32 -04:00
parent 83650961f8
commit a574c68975
4 changed files with 100 additions and 46 deletions

71
dist/epub.js vendored
View file

@ -2745,9 +2745,8 @@ EPUBJS.Queue.prototype.dequeue = function(){
if(this._q.length) { if(this._q.length) {
inwait = this._q.shift(); inwait = this._q.shift();
task = inwait.task; task = inwait.task;
//console.log(task)
if(task){ if(task){
// Task is a function that returns a promise // Task is a function that returns a promise
return task.apply(this.context, inwait.args).then(function(){ return task.apply(this.context, inwait.args).then(function(){
inwait.deferred.resolve.apply(inwait.context || this.context, arguments); inwait.deferred.resolve.apply(inwait.context || this.context, arguments);
@ -5307,7 +5306,7 @@ EPUBJS.Continuous.prototype.afterDisplayedAbove = function(currView){
return; return;
} }
// bounds = currView.bounds(); // bounds = currView.bounds();
console.log("afterDisplayedAbove")
if(this.settings.axis === "vertical") { if(this.settings.axis === "vertical") {
this.scrollBy(0, bounds.height, true); this.scrollBy(0, bounds.height, true);
} else { } else {
@ -5481,7 +5480,7 @@ EPUBJS.Continuous.prototype.check = function(){
clearTimeout(this.trimTimeout); clearTimeout(this.trimTimeout);
this.trimTimeout = setTimeout(function(){ this.trimTimeout = setTimeout(function(){
this.trim(); this.q.enqueue(this.trim);
}.bind(this), 250); }.bind(this), 250);
checking.resolve(); checking.resolve();
@ -5490,6 +5489,7 @@ EPUBJS.Continuous.prototype.check = function(){
}; };
EPUBJS.Continuous.prototype.trim = function(){ EPUBJS.Continuous.prototype.trim = function(){
var task = new RSVP.defer();
var above = true; var above = true;
this.views.forEach(function(view, i){ this.views.forEach(function(view, i){
@ -5498,17 +5498,19 @@ EPUBJS.Continuous.prototype.trim = function(){
var nextShown = (i+1 < this.views.length) ? this.views[i+1].shown : false; var nextShown = (i+1 < this.views.length) ? this.views[i+1].shown : false;
if(!view.shown && !prevShown && !nextShown) { if(!view.shown && !prevShown && !nextShown) {
// Remove // Remove
//this.q.enqueue(this.erase, view, above);
this.erase(view, above); this.erase(view, above);
} }
if(nextShown) { if(nextShown) {
above = false; above = false;
} }
}.bind(this)); }.bind(this));
task.resolve();
return task.promise;
}; };
EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim
// remove from dom
var prevTop = this.container.scrollTop; var prevTop = this.container.scrollTop;
var prevLeft = this.container.scrollLeft; var prevLeft = this.container.scrollLeft;
var bounds = view.bounds(); var bounds = view.bounds();
@ -5516,16 +5518,13 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim
this.remove(view); this.remove(view);
if(above) { if(above) {
console.log("erase")
if(this.settings.axis === "vertical") { if(this.settings.axis === "vertical") {
this.scrollTo(0, prevTop - bounds.height, true); this.scrollTo(0, prevTop - bounds.height, true);
} else { } else {
this.scrollTo(prevLeft -bounds.width, 0, true); this.scrollTo(prevLeft - bounds.width, 0, true);
} }
} }
// task.resolve();
// return task.promise;
}; };
@ -5593,6 +5592,11 @@ EPUBJS.Continuous.prototype.start = function() {
this.tick = EPUBJS.core.requestAnimationFrame; this.tick = EPUBJS.core.requestAnimationFrame;
this.prevScrollTop = this.container.scrollTop;
this.prevScrollLeft = this.container.scrollLeft;
this.scrollDeltaVert = 0;
this.scrollDeltaHorz = 0;
this.container.addEventListener("scroll", function(e){ this.container.addEventListener("scroll", function(e){
if(!this.ignore) { if(!this.ignore) {
this.scrolled = true; this.scrolled = true;
@ -5613,24 +5617,47 @@ EPUBJS.Continuous.prototype.start = function() {
EPUBJS.Continuous.prototype.onScroll = function(){ EPUBJS.Continuous.prototype.onScroll = function(){
if(this.scrolled && !this.ignore) { if(this.scrolled) {
scrollTop = this.container.scrollTop; scrollTop = this.container.scrollTop;
scrollLeft = this.container.scrollLeft; scrollLeft = this.container.scrollLeft;
this.trigger("scroll", { if(!this.ignore) {
top: scrollTop,
left: scrollLeft
});
this.q.enqueue(this.check); this.trigger("scroll", {
top: scrollTop,
left: scrollLeft
});
if(this.scrollDeltaVert === 0 ||
this.scrollDeltaHorz === 0 ||
this.scrollDeltaVert > this.settings.offset / 2 ||
this.scrollDeltaHorz > this.settings.offset / 2) {
this.q.enqueue(this.check);
this.scrollDeltaVert = 0;
this.scrollDeltaHorz = 0;
}
} else {
this.ignore = false;
}
this.scrollDeltaVert += Math.abs(scrollTop-this.prevScrollTop);
this.scrollDeltaHorz += Math.abs(scrollLeft-this.prevScrollLeft);
this.prevScrollTop = this.container.scrollTop;
this.prevScrollLeft = this.container.scrollLeft;
clearTimeout(this.scrollTimeout);
this.scrollTimeout = setTimeout(function(){
this.scrollDeltaVert = 0;
this.scrollDeltaHorz = 0;
}.bind(this), 150);
//this.prevScrollTop = scrollTop;
//this.prevScrollLeft = scrollLeft;
this.scrolled = false;
} else {
this.ignore = false;
this.scrolled = false; this.scrolled = false;
} }

4
dist/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -124,7 +124,7 @@ EPUBJS.Continuous.prototype.afterDisplayedAbove = function(currView){
return; return;
} }
// bounds = currView.bounds(); // bounds = currView.bounds();
console.log("afterDisplayedAbove")
if(this.settings.axis === "vertical") { if(this.settings.axis === "vertical") {
this.scrollBy(0, bounds.height, true); this.scrollBy(0, bounds.height, true);
} else { } else {
@ -298,7 +298,7 @@ EPUBJS.Continuous.prototype.check = function(){
clearTimeout(this.trimTimeout); clearTimeout(this.trimTimeout);
this.trimTimeout = setTimeout(function(){ this.trimTimeout = setTimeout(function(){
this.trim(); this.q.enqueue(this.trim);
}.bind(this), 250); }.bind(this), 250);
checking.resolve(); checking.resolve();
@ -307,6 +307,7 @@ EPUBJS.Continuous.prototype.check = function(){
}; };
EPUBJS.Continuous.prototype.trim = function(){ EPUBJS.Continuous.prototype.trim = function(){
var task = new RSVP.defer();
var above = true; var above = true;
this.views.forEach(function(view, i){ this.views.forEach(function(view, i){
@ -315,17 +316,19 @@ EPUBJS.Continuous.prototype.trim = function(){
var nextShown = (i+1 < this.views.length) ? this.views[i+1].shown : false; var nextShown = (i+1 < this.views.length) ? this.views[i+1].shown : false;
if(!view.shown && !prevShown && !nextShown) { if(!view.shown && !prevShown && !nextShown) {
// Remove // Remove
//this.q.enqueue(this.erase, view, above);
this.erase(view, above); this.erase(view, above);
} }
if(nextShown) { if(nextShown) {
above = false; above = false;
} }
}.bind(this)); }.bind(this));
task.resolve();
return task.promise;
}; };
EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim
// remove from dom
var prevTop = this.container.scrollTop; var prevTop = this.container.scrollTop;
var prevLeft = this.container.scrollLeft; var prevLeft = this.container.scrollLeft;
var bounds = view.bounds(); var bounds = view.bounds();
@ -333,16 +336,13 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim
this.remove(view); this.remove(view);
if(above) { if(above) {
console.log("erase")
if(this.settings.axis === "vertical") { if(this.settings.axis === "vertical") {
this.scrollTo(0, prevTop - bounds.height, true); this.scrollTo(0, prevTop - bounds.height, true);
} else { } else {
this.scrollTo(prevLeft -bounds.width, 0, true); this.scrollTo(prevLeft - bounds.width, 0, true);
} }
} }
// task.resolve();
// return task.promise;
}; };
@ -410,6 +410,11 @@ EPUBJS.Continuous.prototype.start = function() {
this.tick = EPUBJS.core.requestAnimationFrame; this.tick = EPUBJS.core.requestAnimationFrame;
this.prevScrollTop = this.container.scrollTop;
this.prevScrollLeft = this.container.scrollLeft;
this.scrollDeltaVert = 0;
this.scrollDeltaHorz = 0;
this.container.addEventListener("scroll", function(e){ this.container.addEventListener("scroll", function(e){
if(!this.ignore) { if(!this.ignore) {
this.scrolled = true; this.scrolled = true;
@ -430,24 +435,47 @@ EPUBJS.Continuous.prototype.start = function() {
EPUBJS.Continuous.prototype.onScroll = function(){ EPUBJS.Continuous.prototype.onScroll = function(){
if(this.scrolled && !this.ignore) { if(this.scrolled) {
scrollTop = this.container.scrollTop; scrollTop = this.container.scrollTop;
scrollLeft = this.container.scrollLeft; scrollLeft = this.container.scrollLeft;
this.trigger("scroll", { if(!this.ignore) {
top: scrollTop,
left: scrollLeft
});
this.q.enqueue(this.check); this.trigger("scroll", {
top: scrollTop,
left: scrollLeft
});
if(this.scrollDeltaVert === 0 ||
this.scrollDeltaHorz === 0 ||
this.scrollDeltaVert > this.settings.offset / 2 ||
this.scrollDeltaHorz > this.settings.offset / 2) {
this.q.enqueue(this.check);
this.scrollDeltaVert = 0;
this.scrollDeltaHorz = 0;
}
} else {
this.ignore = false;
}
this.scrollDeltaVert += Math.abs(scrollTop-this.prevScrollTop);
this.scrollDeltaHorz += Math.abs(scrollLeft-this.prevScrollLeft);
this.prevScrollTop = this.container.scrollTop;
this.prevScrollLeft = this.container.scrollLeft;
clearTimeout(this.scrollTimeout);
this.scrollTimeout = setTimeout(function(){
this.scrollDeltaVert = 0;
this.scrollDeltaHorz = 0;
}.bind(this), 150);
//this.prevScrollTop = scrollTop;
//this.prevScrollLeft = scrollLeft;
this.scrolled = false;
} else {
this.ignore = false;
this.scrolled = false; this.scrolled = false;
} }

View file

@ -51,9 +51,8 @@ EPUBJS.Queue.prototype.dequeue = function(){
if(this._q.length) { if(this._q.length) {
inwait = this._q.shift(); inwait = this._q.shift();
task = inwait.task; task = inwait.task;
//console.log(task)
if(task){ if(task){
// Task is a function that returns a promise // Task is a function that returns a promise
return task.apply(this.context, inwait.args).then(function(){ return task.apply(this.context, inwait.args).then(function(){
inwait.deferred.resolve.apply(inwait.context || this.context, arguments); inwait.deferred.resolve.apply(inwait.context || this.context, arguments);