更改路由后,运行一次js脚本

After Route Change, run js script once

本文关键字:一次 js 脚本 路由 运行      更新时间:2023-09-26

我正在尝试实现一个系统,在路由更改后,我只想运行一次基于动画的javascript。现在发生的情况是,如果您在页面之间切换,然后返回到关于页面。涂鸦真的减慢了网站的速度,这正是我试图避免的。

app.controller("aboutController",函数($scope,$route){

$scope.$on('$routeChangeSuccess', function() { 

  // Just need this to run once
  $('#about').particleground({
      dotColor: '#666',
      lineColor: '#666',
      lineWidth: .3,
      particleRadius: 3,
      parallaxMultiplier: 3
  });

});

在$routeChangeSuccess`中运行动画之前,请检查currentRoute.redirectTo。它将阻止动画多次运行。

$scope.$on("$routeChangeSuccess", function(event, currentRoute, previousRoute){
  if(!currentRoute.redirectTo) {
              $('#about').particleground({
                dotColor: '#666',
                lineColor: '#666',
                lineWidth: .3,
                particleRadius: 3,
                parallaxMultiplier: 3
             });
       }
});

它执行两次的原因是:

在您的HTML:中

<a href="#about">›&nbsp;&nbsp;About Me</a>

在href中,您只有#about,但它必须是#/about

<a href="#/about">›&nbsp;&nbsp;About Me</a>