我正在尝试在 famo.us 中构建一个粘性滚动视图,但我被困在两个地方。
1)正文在标题顶部滚动。当我在将其添加到 _mainScrollview 之前将 Transform.translate(0, 0, 1) 添加到标题时,它什么也不做。
2) 当 _mainScrollview 卡住时,如何保持 _bodyScrollview 的速度?
/***
* A view that scrolls, sticks the header at stickAt,
* and then allows the body to scroll.
*/
function Sticky(options) {
View.apply(this, arguments);
options = options || {};
this.options = Object.create(Sticky.DEFAULT_OPTIONS);
for (var i in Sticky.DEFAULT_OPTIONS) {
if (options[i] !== undefined) this.options[i] = options[i];
}
// setup the scrollviews
this._bodyScrollview = new Scrollview({});
this._bodyScrollview.sequenceFrom([this.options.body]);
this._mainScrollview = new Scrollview({});
this._mainScrollview.sequenceFrom([this.options.header, this._bodyScrollview]);
this._eventInput.pipe(this._mainScrollview);
// track the velocities
this._mainScrollview.sync.on('update', function (data) {
this._mainVelocity = data.velocity;
}.bind(this));
this._bodyScrollview.sync.on('update', function (data) {
this._bodyVelocity = data.velocity;
}.bind(this));
this.add(this._mainScrollview);
}
Sticky.prototype.render = function () {
// If the main scrollview is scrolled up (velocity < 0)
// past the stickAt point, stick it and unstick the body scrollview.
if (this._mainVelocity < 0) {
if (this._mainScrollview.getPosition() >= this.options.stickAt) {
this._eventInput.unpipe(this._mainScrollview);
this._eventInput.pipe(this._bodyScrollview);
Tools.forcePosition(this._mainScrollview, this.options.stickAt, true);
this._mainVelocity = 0;
}
}
// If the main scrollview is scrolled down (velocity > 0)
// past 0, stick it and unstick the main scrollview.
if (this._bodyVelocity > 0) {
console.log(this._bodyScrollview.getPosition());
if (this._bodyScrollview.getPosition() <= 0) {
this._eventInput.unpipe(this._bodyScrollview);
this._eventInput.pipe(this._mainScrollview);
Tools.forcePosition(this._bodyScrollview, 0, true);
this._bodyVelocity = 0;
}
}
return View.prototype.render.call(this);
};
/**
* Force a scrollview to a position
*/
Tools.forcePosition = function (scrollview, position, noSpring) {
if (noSpring) {
scrollview._springState = 0;
scrollview._physicsEngine.detachAll();
}
scrollview.setVelocity(0);
scrollview.setPosition(position);
};