简体   繁体   English

如何在不破坏任何功能的情况下使用 barba.js 运行其他 JS 文件

[英]How to run other JS-files with barba.js with out breaking any functions

I am using barba.js for page transitions but when I use it my other js-files break.我正在使用 barba.js 进行页面转换,但是当我使用它时,我的其他 js 文件会中断。

Here is my Website这是我的网站

I created a runScript function which I implemented on all other JS-files but it's still not working.我创建了一个runScript function,我在所有其他 JS 文件上实现了它,但它仍然无法正常工作。

what am I doing wrong and how can I fix it?我做错了什么,我该如何解决?

Here is the runscript function on one of the JS-files:这是其中一个 JS 文件上的运行脚本 function:

 const runScript = () => { const footer = document.querySelector('footer'); barba.hooks.leave((data) => { footer.style.opacity = 0; }); barba.hooks.afterEnter((data) => { footer.style.opacity = 1; }); } runScript() barba.init({ transitions: [ { name: "switch", leave({ current, next, trigger }) { }, enter({ current, next, trigger }) { runScript() runScriptc() return new Promise(resolve => { setTimeout(resolve, 2000) }) } } ], views: [], debug: true })

I'm assuming you're using BarbaJs v2.我假设您使用的是 BarbaJs v2。 In that case you don't need to declare that footer behavior in every js file.在这种情况下,您不需要在每个 js 文件中声明该页脚行为。 You are not assigning transition from-to properties, also, you are missing some brackets and semicolon.您没有分配transition from-to ,而且您缺少一些括号和分号。 You just need one js file (or typescript transpiled file) to get that working.您只需要一个 js 文件(或 typescript 转译文件)即可使其正常工作。 Try using this:尝试使用这个:

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 properties allows you to declare a behaviour every time a section enters, or leave . Barba.hooks属性允许您在每次enters, or leave部分时声明一个行为。

Remember always set barba-barba="wrapper" , data-barba="container" and data-barba-namespace="homepage" (or about or contact) tags at the HTML pages you'll use.请记住始终在您将使用的 HTML 页面上设置 barba barba-barba="wrapper"data-barba="container"data-barba-namespace="homepage" (or about or contact)标签。 Eg:例如:

<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>

You can check BarbaJs v2 documentation for more information about transitions.您可以查看BarbaJs v2 文档以获取有关转换的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM