【问题标题】:Why my second script does'nt know the function in the first script?为什么我的第二个脚本不知道第一个脚本中的函数?
【发布时间】: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


【解决方案1】:

//First js
function myFunction(x) {
    var opacity = x.options[x.selectedIndex].value;
    var el = document.getElementById("p1");
    if (el.style.opacity !== undefined) {
        el.style.opacity = opacity;
    } else {
        alert("Your browser doesn't support this!");
    }
}

//Second js
function myFunction() {
    var x = document.getElementById("myTopnav");
        if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}

【讨论】:

  • 我应该明白什么?
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2021-01-15
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 2013-07-15
相关资源
最近更新 更多