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

时间:2020-05-26 19:26:33

标签: javascript html jquery css navigation

我的index.html一切正常。在第一个.onload页面上,所有图像都是可拖动的,并且整个页面上

问题所在:

但是,当我转到ABOUT.html页面并返回到INDEX.html 时,我的脚本似乎不再起作用,并且我只能看到一张甚至无法拖动的图像不再。

在这里仔细查看我的项目: https://codepen.io/cat999/project/editor/AEeEdg

everything work fine on first onload page and images are draggable but when i go to the about page and come back on the index page the draggable is not working

**我该如何解决问题?有人可以帮助我修改代码吗? **

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)");

此外,索引和about部分使用barba.js。 这是我的脚本列表:

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

2 个答案:

答案 0 :(得分:1)

Marlene,请尝试在barba.init函数内的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);
        }, 
    }],

答案 1 :(得分:0)

Marlene,您需要将此JS代码放在下面的单独的JS文件中,并将其命名为draggable.js,例如,然后仅在上述代码示例中说明的script.src参数中插入该JS文件的路径。在barba.init函数中。


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)");