simplify scroll animations

This commit is contained in:
nightwing 2012-04-24 22:10:50 +04:00
commit c72357c201
2 changed files with 54 additions and 62 deletions

View file

@ -108,7 +108,8 @@ var VirtualRenderer = function(container, theme) {
this.scrollBar = new ScrollBar(container);
this.scrollBar.addEventListener("scroll", function(e) {
_self.session.setScrollTop(e.data);
if (!_self.$inScrollAnimation)
_self.session.setScrollTop(e.data);
});
this.scrollTop = 0;
@ -746,37 +747,53 @@ var VirtualRenderer = function(container, theme) {
for (i = 0; i < l; ++i)
steps.push(func(i / this.STEPS, fromValue, toValue - fromValue));
steps.push(toValue);
return steps;
};
this.scrollToLine = function(line, center, animate, callback) {
this.scrollToLine = function(line, center, animate) {
var pos = this.$cursorLayer.getPixelPosition({row: line, column: 0});
var offset = pos.top;
if (center)
offset -= this.$size.scrollerHeight / 2;
if (animate !== false
&& this.$animatedScroll && Math.abs(offset - this.scrollTop) < 100000) {
var _self = this;
var steps = _self.$calcSteps(this.scrollTop, offset);
clearInterval(this.$timer);
this.$timer = setInterval(function() {
_self.session.setScrollTop(steps.shift());
if (!steps.length) {
callback && callback();
clearInterval(_self.$timer);
}
}, 10);
}
else {
if (animate) {
this.animateScrolling(function() {
this.session.setScrollTop(offset);
}, this);
} else {
this.session.setScrollTop(offset);
}
};
this.animateScrolling = function(scrollFunc, self) {
var fromValue = this.scrollTop;
scrollFunc.call(self);
var toValue = this.scrollTop;
if (this.$animatedScroll && Math.abs(fromValue - toValue) < 100000) {
var _self = this;
var steps = _self.$calcSteps(fromValue, toValue);
this.$inScrollAnimation = true;
clearInterval(this.$timer);
_self.session.setScrollTop(steps.shift());
this.$timer = setInterval(function() {
if (steps.length) {
_self.session.setScrollTop(steps.shift());
// trick session to think it's already scrolled to not loose toValue
_self.session.$scrollTop = toValue;
} else {
this.$inScrollAnimation = false;
clearInterval(_self.$timer);
_self.session.$scrollTop = -1;
_self.session.setScrollTop(toValue);
}
}, 10);
}
};
this.scrollToY = function(scrollTop) {
// after calling scrollBar.setScrollTop
// scrollbar sends us event with same scrollTop. ignore it