The function ScrollToStage is using the function waitForScrollEnd
defined in the first script. If I don't copy this function here it doesn't work, I got a reference error function waitForScrollEnd is not defined
...
It's very ugly, I can't let my code like that. What can I do ?
@push('scripts')
<script src="{{asset('js/home/home.js')}}"></script>
<script>
function scrollHandler(element = null) {
return {
height: 0,
element: element,
calculateHeight(position) {
const distanceFromTop = this.element.offsetTop
const contentHeight = this.element.clientHeight
const visibleContent = contentHeight - window.innerHeight
const start = Math.max(0, position - distanceFromTop)
const percent = (start / visibleContent) * 100;
requestAnimationFrame(() => {
this.height = percent;
});
},
init() {
this.element = this.element || document.body;
this.calculateHeight(window.scrollY);
}
};
}
let listFormations = document.getElementById('list-formations');
function scrollToStage(upOrDown) {
let myCurrentStage = parseInt(listFormations.__x.$data.currentStage);
console.log('stage'+(myCurrentStage+1));
let nextStage = document.getElementById('stage'+(myCurrentStage+1));
let previousStage = document.getElementById('stage'+(myCurrentStage-1));
if (upOrDown === 'down'){
//console.log(nextStage);
if(typeof(nextStage) != 'undefined' && nextStage != null){
nextStage.scrollIntoView({behavior: "smooth"});
callbackAfterScroll = () => {
window.scrollTo({top: window.pageYOffset-175, behavior:"smooth"});
}
waitScrollEnd();
listFormations.__x.$data.currentStage++;
}
}else if(upOrDown === 'home'){
document.getElementById('stage0').scrollIntoView({behavior: "smooth"});
callbackAfterScroll = () => {
window.scrollTo({top: window.pageYOffset-300, behavior:"smooth"});
}
waitScrollEnd();
listFormations.__x.$data.currentStage = 0 ;
}
else{
//console.log(previousStage);
if(typeof(previousStage) != 'undefined' && previousStage != null){
previousStage.scrollIntoView({behavior: "smooth"});
callbackAfterScroll = () => {
window.scrollTo({top: window.pageYOffset-175, behavior:"smooth"});
}
waitScrollEnd();
listFormations.__x.$data.currentStage--;
}
}
}
let timer = null
function waitScrollEnd(){
timer = null
window.addEventListener( 'scroll', listenScroll,true);
}
function listenScroll(){
clearTimeout(timer)
timer = setTimeout( () => {
/*console.log("scroll stop")*/
callbackAfterScroll();
window.removeEventListener( 'scroll', listenScroll,true);
callbackAfterScroll = null
}, 100 )
}
</script>
@endpush
Here is waitScrollEnd in home.js it's exactly the same but if I don't copy it, it doesn't work :
let timer = null
function waitScrollEnd(){
timer = null
window.addEventListener( 'scroll', listenScroll,true);
}
function listenScroll(){
clearTimeout(timer)
timer = setTimeout( () => {
/*console.log("scroll stop")*/
callbackAfterScroll();
window.removeEventListener( 'scroll', listenScroll,true);
callbackAfterScroll = null
}, 100 )
}
I checked waitScrollEnd is present in home.js and loaded and I got this error : Alpine Error: "ReferenceError: waitScrollEnd is not defined"