简体   繁体   English

使用barba.js进行页面转换后无法加载JavaScript文件

[英]JavaScript file doesn't load after page transition with barba.js

I have a website with a link. 我有一个带有链接的网站。 When I click the link I want a smooth page transition to the next page using jQuery. 当我单击链接时,我希望使用jQuery将页面平滑过渡到下一页。 I got the the HTML elements inside the barba DOM to fade out. 我得到了barba DOM中的HTML元素淡出。 But when the next page tries to load, all BUT the JavaScript loads. 但是,当下一页尝试加载时,所有JavaScript都将加载。

This is my code: (index.html) 这是我的代码:(index.html)

   <div id="barba-wrapper" class="wrapper">
    <div class="barba-container">
        <div class="container">
            <span class="text1">Hey There!</span>
            <span class="text2">This is my portfolio!</span>
            <a id="homeLink" href="./home.html">Click me to continue</a>
        </div>
    </div>
</div>
<script>
    $('document').ready(function(){
        var transEffect = Barba.BaseTransition.extend({
            start: function(){
              this.newContainerLoading.then(val => this.fadeInNewcontent($(this.newContainer)));
            },
            fadeInNewcontent: function(nc) {
              nc.hide();
              console.log('stuff should be hidden');
              var $el = $(this.newContainer);
              var _this = this;
              $(this.oldContainer).fadeOut(1000).promise().done(() => {
                nc.css('visibility', 'visible');
                nc.fadeIn(1000, function(){
                console.log('new content should fade in.');
                _this.done();
                })
              });
            }
        });
        Barba.Pjax.getTransition = function() {
          return transEffect;
        }
        Barba.Pjax.start();
    });
</script>

I can see the the console logs in the console but JavaScript doesn't load. 我可以在控制台中看到控制台日志,但未加载JavaScript。

This is the page I'm trying to load: (home.html) 这是我要加载的页面:(home.html)

<div id="barba-wrapper" class="wrapper">
    <div class="barba-container">
        <canvas></canvas>
        <script src="canvas.js"></script>
    </div>
</div>

I get no errors whatsoever. 我没有任何错误。 Also I have seen a question on here with the same problem as mine, but it was very unclear. 我在这里也看到了一个与我的问题相同的问题,但是还不清楚。 (and does not have any answers so far) Thanks! (并且到目前为止没有任何答案)谢谢!

In Barba, scripts don't evaluated when loading a new container. 在Barba中,加载新容器时不会评估脚本。 It can be done easily using Events . 使用Events可以很容易地做到这一点。

Barba.Dispatcher.on('newPageReady', function (currentStatus, oldStatus, container) {
    $(container).find('script').each(function (i, script) {
        var $script = $(script);
        $.ajax({
            url: $script.attr('src'),
            cache: true,
            dataType: 'script',
            success: function () {
                $script.trigger('load');
            }
        });
    });
});

Add this line: 添加此行:

document.getElementsByTagName("head")[0].innerHTML += "<meta http-equiv=\"refresh\" content=\"1\">"; 

inside below function 在下面的功能

fadeInNewcontent: function(nc) {

}

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

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