【问题标题】:JavaScript file doesn't load after page transition (barba.js)页面转换后 JavaScript 文件不加载 (barba.js)
【发布时间】:2020-05-26 19:26:33
【问题描述】:

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

问题出在这里:

但是,当我转到页面 ABOUT.html 并返回到 INDEX.html 时,我的脚本看起来不再工作了,我只能看到一个甚至无法拖动的图像没有了。

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

** 如何解决问题?有人可以帮我修复我的代码吗? **

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

此外,index 和 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>

【问题讨论】:

    标签: javascript html jquery css navigation


    【解决方案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);
            }, 
        }],
    

    【讨论】:

    • 谢谢@timur,你能告诉我我必须将代码放在 main.js 的什么地方吗?这里是代码srv-file8.gofile.io/download/JE7Rwp/main.js
    • 当我将此代码添加到 barba.init views: [{ namespace: 'home-section', beforeEnter({ next }) { // load your script let script = document.createElement('script'); script.src = 'https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.4/TweenMax.min.js'; script.src = 'https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.4/utils/Draggable.min.js'; script.src = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/ThrowPropsPlugin.min.js'; next.container.appendChild(script); }, }], 时没有任何反应
    【解决方案2】:

    Marlene,您需要将此 JS 代码放在下面的一个单独的 JS 文件中,并将其命名为 draggable.js,然后仅将该 JS 文件的路径插入到上面代码示例中所述的 script.src 参数中在 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)");
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-03
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 2021-02-27
      • 1970-01-01
      相关资源
      最近更新 更多