使用 barba.js 将滚动位置设置为新页面的顶部
Set scroll position to top in new page using barba.js
我使用 barba.js 文档中的代码如下在页面之间切换。
var FadeTransition = Barba.BaseTransition.extend({
start: function() {
Promise
.all([this.newContainerLoading, this.fadeOut()])
.then(this.fadeIn.bind(this));
},
fadeOut: function() {
return $(this.oldContainer).animate({ opacity: 0 }).promise();
},
fadeIn: function() {
var _this = this;
var $el = $(this.newContainer);
$(this.oldContainer).hide();
$el.css({
visibility : 'visible',
opacity : 0
});
$el.animate({ opacity: 1 }, 400, function() {
_this.done();
});
}
});
Barba.Pjax.getTransition = function() {
return FadeTransition;
};
问题是在新页面中保留了滚动位置。
我试图添加 $(window).scrollTop(0);在 fadeIn 函数中,但这会在按下后退按钮时产生不需要的滚动。如何解决?
下面是添加$(window).scrollTop(0);
后的行为
- 在页面A中,向下滚动并按link到页面B。页面B进入并在顶部滚动位置
- 按返回键,B页滚动到A页位置后淡出
- 页面 A 进入并在顶部滚动位置
- 向下滚动并按前进按钮。页面A滚动到顶部然后淡出
- 页面 B 进入并在顶部滚动位置
以下是预期的行为:
- 在页面A中,向下滚动并按link到页面B。页面B进入并在顶部滚动位置
- 按下后退按钮,页面 B 淡出而不滚动
- 页面A进入第1步滚动位置
fadeIn: function() {
$(window).scrollTop(0); // scroll to top here
var _this = this;
var $el = $(this.newContainer);
$(this.oldContainer).hide();
$el.css({
visibility : 'visible',
opacity : 0
});
$el.animate({ opacity: 1 }, 400, function() {
/**
* Do not forget to call .done() as soon your transition is finished!
* .done() will automatically remove from the DOM the old Container
*/
_this.done();
});
Barba.Dispatcher.on('newPageReady', function(current, prev, container) {
history.scrollRestoration = 'manual';
});
添加这个解决了跳转问题。但是,这样会丢失最后一页的滚动位置,即无论进入新页面还是旧页面,总是在最前面。
我是这样工作的:
fadeIn: function() {
/**
* this.newContainer is the HTMLElement of the new Container
* At this stage newContainer is on the DOM (inside our #barba-container and with visibility: hidden)
* Please note, newContainer is available just after newContainerLoading is resolved!
*/
var _this = this;
var $el = $(this.newContainer);
$(this.oldContainer).hide();
$el.css({
visibility : 'visible',
opacity : 0
});
$("html, body").animate({ scrollTop: 0 }, "slow");
$el.animate({ opacity: 1 }, 400, function() {
_this.done();
});
我使用 barba.js 文档中的代码如下在页面之间切换。
var FadeTransition = Barba.BaseTransition.extend({
start: function() {
Promise
.all([this.newContainerLoading, this.fadeOut()])
.then(this.fadeIn.bind(this));
},
fadeOut: function() {
return $(this.oldContainer).animate({ opacity: 0 }).promise();
},
fadeIn: function() {
var _this = this;
var $el = $(this.newContainer);
$(this.oldContainer).hide();
$el.css({
visibility : 'visible',
opacity : 0
});
$el.animate({ opacity: 1 }, 400, function() {
_this.done();
});
}
});
Barba.Pjax.getTransition = function() {
return FadeTransition;
};
问题是在新页面中保留了滚动位置。 我试图添加 $(window).scrollTop(0);在 fadeIn 函数中,但这会在按下后退按钮时产生不需要的滚动。如何解决?
下面是添加$(window).scrollTop(0);
后的行为- 在页面A中,向下滚动并按link到页面B。页面B进入并在顶部滚动位置
- 按返回键,B页滚动到A页位置后淡出
- 页面 A 进入并在顶部滚动位置
- 向下滚动并按前进按钮。页面A滚动到顶部然后淡出
- 页面 B 进入并在顶部滚动位置
以下是预期的行为:
- 在页面A中,向下滚动并按link到页面B。页面B进入并在顶部滚动位置
- 按下后退按钮,页面 B 淡出而不滚动
- 页面A进入第1步滚动位置
fadeIn: function() {
$(window).scrollTop(0); // scroll to top here
var _this = this;
var $el = $(this.newContainer);
$(this.oldContainer).hide();
$el.css({
visibility : 'visible',
opacity : 0
});
$el.animate({ opacity: 1 }, 400, function() {
/**
* Do not forget to call .done() as soon your transition is finished!
* .done() will automatically remove from the DOM the old Container
*/
_this.done();
});
Barba.Dispatcher.on('newPageReady', function(current, prev, container) {
history.scrollRestoration = 'manual';
});
添加这个解决了跳转问题。但是,这样会丢失最后一页的滚动位置,即无论进入新页面还是旧页面,总是在最前面。
我是这样工作的:
fadeIn: function() {
/**
* this.newContainer is the HTMLElement of the new Container
* At this stage newContainer is on the DOM (inside our #barba-container and with visibility: hidden)
* Please note, newContainer is available just after newContainerLoading is resolved!
*/
var _this = this;
var $el = $(this.newContainer);
$(this.oldContainer).hide();
$el.css({
visibility : 'visible',
opacity : 0
});
$("html, body").animate({ scrollTop: 0 }, "slow");
$el.animate({ opacity: 1 }, 400, function() {
_this.done();
});