javascript - 使用 fullpage.js 重置 CSS 动画

标签 javascript jquery animation fullpage.js animate.css

我目前正在建立一个网站 fullPage.jsanimate.css 。我试图在每次滚动到元素显示的部分时重播 animate.css 中的 CSS 动画。

问题是:在我滚动到下一部分后它滑出,但是当我再次返回该部分时,该元素消失并且不再滑入。

这是我尝试过的方法(没有正常工作):

$('#fullpage').fullpage({
  onLeave: function(index, nextIndex, direction) {
    $('#text').removeClass('animated slideInLeft');
    $('#text').addClass('animated slideOutRight');
  },
  afterLoad: function(anchorLink, index) {
    $('#text').addClass('animated slideInLeft');
  }
});
#first {
  background-color: yellow;
}
#second {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/2.7.9/jquery.fullPage.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.1/animate.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/2.7.9/jquery.fullPage.css" rel="stylesheet" />

<div id="fullpage">

  <div class="section" id="first">
    <center>
      <p id="text">Something</p>
    </center>
  </div>

  <div class="section" id="second">
    <h2>Something else</h2>
  </div>

</div>

最佳答案

您可以区分自己在哪张幻灯片上。之后就可以设置相应的类了。看例子:

onLeave: function(index, nextIndex, direction) {
  if(nextIndex === 2) {
    $('#text').addClass('slideOutRight');
  }

  if(index === 2) {
    $('#text').removeClass('slideOutRight');
  }
}

尝试一下:

$('#fullpage').fullpage({
  onLeave: function(index, nextIndex, direction) {
    if(nextIndex === 2) {
      $('#text').addClass('slideOutRight');
    }
    
    if(index === 2) {
      $('#text').removeClass('slideOutRight');  
    }

    console.log('index:', index, 'nextIndex:', nextIndex, 'direction:', direction);
  },
  afterLoad: function(anchorLink, index) {
    $('#text').addClass('animated slideInLeft');
  }
});
#first {
  background-color: yellow;
}
#second {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/2.7.9/jquery.fullPage.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.1/animate.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/2.7.9/jquery.fullPage.css" rel="stylesheet" />

<div id="fullpage">

  <div class="section" id="first">
    <center>
      <p id="text">Something</p>
    </center>
  </div>

  <div class="section" id="second">
    <h2>Something else</h2>
  </div>

</div>

关于javascript - 使用 fullpage.js 重置 CSS 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37397666/

相关文章:

jQuery 使用两个按钮在三种不同样式之间更改单个 div 的类

javascript - 使用jquery动画进行div移动

javascript - 我如何开始使用 JavaScriptMVC?

javascript - 使用 Object.create() 创建 XMLHttpRequest 对象

jquery - 需要对齐 bootstrap carousel 并使其子弹工作

Android ==> 简单的二维动画?

ios - 如何在 iOS 上的 View 之间进行展开/收缩转换?

javascript - 使用 PHP 和 javascript 查询 MySQL 下拉菜单

javascript - 如何将数据从一个函数传递到作为第一个函数的参数的另一个函数

javascript - 在ajax中发送byte[]