【发布时间】:2021-09-30 22:32:58
【问题描述】:
函数 ScrollToStage 正在使用第一个脚本中定义的函数 waitForScrollEnd。如果我不在这里复制这个函数,它就不起作用,我得到一个reference error function waitForScrollEnd is not defined...
这很丑陋,我不能让我的代码那样。我能做什么?
@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
这是 home.js 中的 waitScrollEnd,它完全一样,但如果我不复制它,它就不起作用:
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 )
}
我检查了 home.js 中是否存在 waitScrollEnd 并已加载,但出现此错误: Alpine 错误:“ReferenceError:waitScrollEnd 未定义”
【问题讨论】:
-
第一个脚本是指
js/home/home.js?如果是这样,它看起来像什么?你的函数waitForScrollEnd是如何定义的? -
在第一个脚本中,waitForScrollEnd 的定义与您看到的一样,但在第二个脚本中复制它以使其正常工作是不正常的。
-
看看js/home/home.js代码就好了。
-
第一个脚本是否正确加载? (检查开发工具网络选项卡)
-
你在谈论
waitForScrollEnd,但在你的代码中我只看到waitScrollEnd
标签: javascript alpine.js