Javascript 如何使用barba.JS运行其他JS文件而不中断任何函数

Javascript 如何使用barba.JS运行其他JS文件而不中断任何函数,javascript,barbajs,Javascript,Barbajs,我使用barba.js进行页面转换,但当我使用它时,我的其他js文件会中断 这是我的 我创建了一个runScript函数,我在所有其他JS文件上实现了该函数,但它仍然不起作用 我做错了什么?我该如何修复它 以下是其中一个JS文件上的runscript函数: const runScript=()=>{ const footer=document.querySelector('footer'); barba.hooks.leave((数据)=>{ footer.style.opacity=0; }

我使用barba.js进行页面转换,但当我使用它时,我的其他js文件会中断

这是我的

我创建了一个runScript函数,我在所有其他JS文件上实现了该函数,但它仍然不起作用

我做错了什么?我该如何修复它

以下是其中一个JS文件上的runscript函数:

const runScript=()=>{
const footer=document.querySelector('footer');
barba.hooks.leave((数据)=>{
footer.style.opacity=0;
});
barba.hooks.afterEnter((数据)=>{
footer.style.opacity=1;
});
}
运行脚本()
巴巴多斯倡议({
过渡:[
{
名称:“交换机”,
离开({current,next,trigger}){
},
输入({current,next,trigger}){
运行脚本()
runScriptc()
返回新承诺(解决=>{
设置超时(解析,2000)
})
}
}
],
视图:[],
调试:正确

})
我假设您使用的是BarbaJs v2。在这种情况下,您不需要在每个js文件中声明页脚行为。您没有指定从到的转换属性,而且缺少一些括号和分号。您只需要一个js文件(或typescript传输文件)就可以让它正常工作。尝试使用以下方法:

const runScript = () => {
  const footer = document.querySelector('footer');

  barba.hooks.beforeLeave((data) => {
      footer.style.opacity = 0;
  });

  barba.hooks.afterEnter((data) => {
      footer.style.opacity = 1;
  });

}


runScript();

barba.init({
    transitions: [{

            name: "switch",

            from: { namespace : [ 'homepage' ] }, //set the "from" namespaces.

            to: { namespace : [ 'about', 'contact' ] }, //set the "to" namespaces.

            beforeLeave({ current, next, trigger }) { //before leaving the page.
                 //Begin your transition and return "resolve" when is completed.
                 return new Promise(resolve => {
                    setTimeout(resolve, 2000);
                 })
            },
            afterEnter({ current, next, trigger }) {
                 //remove the transition (is not necessary to return a promise, at this point barba doesn't check for promises).

            }
        }],
    views: [],
    debug: true
});
Barba.hooks
属性允许您在每次节
进入或离开时声明行为

请记住,在要使用的HTML页面上,始终设置
barba-barba=“wrapper”
data-barba=“container”
data-barba-namespace=“homepage”(或about或contact)
标记。例如:

<div class="main-wrapper" data-barba="wrapper"> 

    <!-- Content that will not change -->

    <section data-barba="container" data-barba-namespace="homepage"> 

        <!-- Content that will change --> 

    </section>
 </div>

您可以查看有关转换的更多信息