Closed Mellis84 closed 5 years ago
work with views an then use onLeaveCompleted
within your detail view to restore the scroll position.
setup base page with anchor(s)
<div class="barba-container">
<div id="achor"></div>
</div>
setup detail view(s) with data-namespace
attribute
<div class="barba-container" data-namespace="detail_view">
.. some stuff
</div>
add a detail view
var detail_view = Barba.BaseView.extend({
namespace: 'detail_view',
onLeaveCompleted: function() {
console.log("detail_view has been removed from DOM");
$('html, body').animate({
scrollTop: $("#anchor").offset().top
}, 10);
}
});
// Don't forget to init the view!
detail_view.init();
Thanks for the fast response dude, I have a transition using the namespace 'detail' on the detail pages. I haven't tried on leave complete though... I can get to the anchor (projects) position when exiting if I specify an element directly... but not sure how to make it more dynamic, like whatever project has exited, the window has the scroll position set to that project.
I guess it would kind of be the reverse of what i have here? I had to target the parent of the lastElementClicked ('.project') for the window to scroll to.
Barba.Dispatcher.on('linkClicked', function(el) {
lastElementClicked = el;
});
// first transition function
scrollToProject: function() {
var _this = this;
var project = lastElementClicked.closest(".project");
var deferred = Barba.Utils.deferred();
TweenLite.to(window, 0.5, {
scrollTo: {
y: project
},
onComplete: function() {
deferred.resolve();
}
});
return deferred.promise;
},`
Sorry if i'm really confusing.
Worked it out! finally.
Had to use localStorage to store the scroll position once the transition has finished scrolling to the project:
First, when the scrollToProject function has completed, store the scroll position
scrollToProject: function() {
var project = $(lastElementClicked).parents('.project');
var deferred = Barba.Utils.deferred();
console.log(project);
TweenLite.to(window, 0.5, {
scrollTo: {
y: project
},
onComplete: function() {
deferred.resolve();
if(typeof(Storage) !== 'undefined') {
// Set scroll position
var lastYPos = +localStorage.getItem('scrollYPos');
localStorage.setItem('scrollYPos', window.scrollY);
}
}
});
return deferred.promise;
}
Then apply that scroll position when leaving the detail page:
var detailView = Barba.BaseView.extend({
namespace: 'detail',
onLeaveCompleted: function() {
$html.style.overflow = 'initial';
if(typeof(Storage) !== 'undefined') {
// Apply set scroll position from the scrollToProject function
var lastYPos = +localStorage.getItem('scrollYPos');
if (lastYPos) {
window.scrollTo(0, lastYPos);
}
}
}
});
Hey hey,
Just have to say I am loving this plugin man. I have an issue though which I hope you can help with, I have a list of projects on a page and when the link inside that project is clicked, the window scrolls to the projects position on the page, then performs the transitions and loads more content for that specific project. What I would like to know is how do I get the window scroll position back to where I initially clicked on the project after the transitions have finished when clicking back out of the project?
I have attached a screenshot and my code, hope you can help! it's been driving me crazy :)
`// set up barba document.addEventListener("DOMContentLoaded", function() {
});`