简体   繁体   English

JavaScript 文件在页面转换后不加载 (barba.js)

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

Everything is working fine on my index.html.在我的 index.html 上一切正常。 On the first.onload page all the images are draggable and all over the page在 first.onload 页面上,所有图像都是可拖动的,并且遍布整个页面

HERE THE PROBLEM:这里的问题:

However, when I WENT TO THE PAGE ABOUT.html AND GO BACK TO THE INDEX.html my script look like isn't working anymore and I can see only one image that is not even draggable anymore.但是,当我进入 ABOUT.html 和 GO 页面时回到 INDEX.html我的脚本看起来不再像可以拖动的图像了

Have a closer look to my project here: https://codepen.io/cat999/project/editor/AEeEdg在这里仔细看看我的项目: https://codepen.io/cat999/project/editor/AEeEdg

在第一个加载页面上一切正常,图像是可拖动的,但是当我转到关于页面并返回索引页面时,可拖动无法正常工作

** HOW DO I FIX ISSUE? ** 如何解决问题? COULD SOMEBODY HELP ME TO FIX MY CODE?有人可以帮我修复我的代码吗? ** **

var winWidth = window.innerWidth,
    winHeight = window.innerHeight,
    threeWinHeight = winHeight*3;

$(window).on('resize', function(){
  winHeight = window.innerHeight,
  twiceWinHeight = winHeight*2;
});


$('.shape').each(function(){
  var topPos = Math.floor(Math.random()*1300),

      //BE CAREFUL 7000px is less then total body height or  CAUSING OVERFLOW
      leftPos = Math.floor(Math.random() * winWidth);
  $(this).css('top', topPos + 'px');
  $(this).css('left', leftPos + 'px');
});




Draggable.create('.shape', {
  //type:"rotation",
  bounds: '.section-2',
  edgeResistance:0.65,
  throwProps:true,
});



var number = Math.floor((Math.random() *15) + 2);
var number2 = Math.floor((Math.random() * 0) + -15);
var number3 = Math.floor((Math.random() * 0) + 15);
var number4 = Math.floor((Math.random() * 0) + 6);
$(".shape--circle").css("transform", "rotate(" + number2 + "deg)");
$(".shape--square").css("transform", "rotate(" + number3 + "deg)");
$(".shape--circle-2").css("transform", "rotate(" + number4 + "deg)");

Additionally, the index and the about section has using barba.js.此外,index 和 about 部分使用 barba.js。 Here the list of my script:这是我的脚本列表:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.4/TweenMax.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.4/utils/Draggable.min.js"></script>
<script type="text/javascript" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/ThrowPropsPlugin.min.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://unpkg.com/@barba/core"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.2.4/gsap.min.js"></script>
<script src="main.js"></script>

Marlene, try adding the following JS code in your main.js file inside barba.init function: Marlene,尝试在 barba.init function 中的 main.js 文件中添加以下 JS 代码:

    views: [{
        namespace: 'home-section',
        beforeEnter({ next }) {

        // load your script
        let script = document.createElement('script');
        script.src = 'draggable.js'; // location of your draggable js file that is responsible for that image loading and dragging functionality
        next.container.appendChild(script);
        }, 
    }],

Marlene, you need to put this JS code below in a separate JS file and name it draggable.js for example and then insert a path to that JS file only into a script.src parameter that is stated in that code example above inside barba.init function. Marlene,您需要将此 JS 代码放在下面的一个单独的 JS 文件中,并将其命名为例如 draggable.js,然后仅将该 JS 文件的路径插入到 barba 中上述代码示例中所述的 script.src 参数中。初始化 function。


var winWidth = window.innerWidth,
    winHeight = window.innerHeight,
    threeWinHeight = winHeight*3;

$(window).on('resize', function(){
  winHeight = window.innerHeight,
  twiceWinHeight = winHeight*2;
});


$('.shape').each(function(){
  var topPos = Math.floor(Math.random()*1300),

      //BE CAREFUL 7000px is less then total body height or  CAUSING OVERFLOW
      leftPos = Math.floor(Math.random() * winWidth);
  $(this).css('top', topPos + 'px');
  $(this).css('left', leftPos + 'px');
});

Draggable.create('.shape', {
  //type:"rotation",
  bounds: '.section-2',
  edgeResistance:0.65,
  throwProps:true,
});


var number = Math.floor((Math.random() *15) + 2);
var number2 = Math.floor((Math.random() * 0) + -15);
var number3 = Math.floor((Math.random() * 0) + 15);
var number4 = Math.floor((Math.random() * 0) + 6);
$(".shape--circle").css("transform", "rotate(" + number2 + "deg)");
$(".shape--square").css("transform", "rotate(" + number3 + "deg)");
$(".shape--circle-2").css("transform", "rotate(" + number4 + "deg)");


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

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