fullpage.js如何重置每页的animate()动画

fullpage.js如何重置每页的animate()动画


使用fullpage.js和jQuery animate()动画的朋友一定会有这样的问题,页面来回滚动时,动画只能执行一遍。
一种解决方法是改用CSS3执行动画,把CSS绑定在section.active上。
但是毕竟CSS3没有jQuery的兼容性好,如果我们需要用到animate()该怎么办呢?
jQuery的animate()更像是CSS3的transition+transform,而不是像animation,animate()是会改变dom样式的

afterLoad: function(anchorLink, index){
    if(index==2){
        $("#exp-1").animate({top:"5%",opacity:"1"},700,function(){
            $("#exp-2").animate({bottom:"0",opacity:"1"},700,function(){
                $("#exp-3").animate({bottom:"0",opacity:"1"},700,function(){
                    $("#exp-4").animate({bottom:"0",opacity:"1"},700)
                });
            })
        })
    }
}

比如上面的代码,当加载完第二页的动画后,再翻到第二页就不会在出现动画,因为CSS值已经达到目标值。
如此一来我们想要重复载入动画的话必须先重置CSS属性。如下代码:

afterLoad: function(anchorLink, index){
    if(index==2){
        $("#exp-1").css({bottom:"-20%",opacity:"0"});
        $("#exp-2").css({bottom:"-20%",opacity:"0"});
        $("#exp-3").css({bottom:"-20%",opacity:"0"});
        $("#exp-4").css({bottom:"-20%",opacity:"0"});
        $("#exp-1").animate({bottom:"0",opacity:"1"},700,function(){
            $("#exp-2").animate({bottom:"0",opacity:"1"},700,function(){
                $("#exp-3").animate({bottom:"0",opacity:"1"},700,function(){
                    $("#exp-4").animate({bottom:"0",opacity:"1"},700)
                });
            })
        })
    }
}

但是这样一来翻到第二页会将CSS重置的过程暴露给用户,用户体验较差,我们可以选择用onLeave来重置CSS:

onLeave: function(index,nextIndex,direction){
    if(nextIndex==2){
        $("#exp-1").css({bottom:"-20%",opacity:"0"});
        $("#exp-2").css({bottom:"-20%",opacity:"0"});
        $("#exp-3").css({bottom:"-20%",opacity:"0"});
        $("#exp-4").css({bottom:"-20%",opacity:"0"});
        $("#exp-1").animate({bottom:"0",opacity:"1"},700,function(){
            $("#exp-2").animate({bottom:"0",opacity:"1"},700,function(){
                $("#exp-3").animate({bottom:"0",opacity:"1"},700,function(){
                    $("#exp-4").animate({bottom:"0",opacity:"1"},700)
                });
            })
        })
    }
}

这样一来animate()动画便可以重复播放。
但另一个问题却暴露出来:页面间快速来回滚动时,未执行完的动画会在滚动回该页面时继续执行。这样也会带来不好的用户体验。我们可以用jQuery的stop()方法。

onLeave: function(index,nextIndex,direction){
    if(nextIndex==2){
        $("#exp-1").css({bottom:"-20%",opacity:"0"});
        $("#exp-2").css({bottom:"-20%",opacity:"0"});
        $("#exp-3").css({bottom:"-20%",opacity:"0"});
        $("#exp-4").css({bottom:"-20%",opacity:"0"});
        $("#exp-1").stop(true,true).animate({bottom:"0",opacity:"1"},700,function(){
            $("#exp-2").stop(true,true).animate({bottom:"0",opacity:"1"},700,function(){
                $("#exp-3").stop(true,true).animate({bottom:"0",opacity:"1"},700,function(){
                    $("#exp-4").stop(true,true).animate({bottom:"0",opacity:"1"},700)
                });
            })
        })
    }
}

这样便大功告成了。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Vue.js 项目中使用 fullpage.js,可以使用 Vue-fullpage.js 插件。下面是一个简单的使用示例: 1. 安装 Vue-fullpage.js 使用 npm 或 yarn 安装 Vue-fullpage.js: ``` npm install vue-fullpage.js ``` 或者 ``` yarn add vue-fullpage.js ``` 2. 在 Vue.js 项目中引入 Vue-fullpage.js 在 main.js 中引入 Vue-fullpage.js: ```javascript import Vue from 'vue' import VueFullpage from 'vue-fullpage.js' Vue.use(VueFullpage) ``` 3. 创建 fullpage 组件 在组件中使用 `<vue-fullpage>` 标签创建 fullpage 组件,然后在组件的 data 中定义页面: ```vue <template> <div> <vue-fullpage :options="options"> <div class="section"> <h1>Page 1</h1> </div> <div class="section"> <h1>Page 2</h1> </div> <div class="section"> <h1>Page 3</h1> </div> </vue-fullpage> </div> </template> <script> export default { data() { return { options: { sectionsColor: ['#f2f2f2', '#4BBFC3', '#7BAABE'] } } } } </script> ``` 在这个示例中,我们创建了一个 fullpage 组件,定义了三个页面,每个页面都是一个 `<div>` 元素,并设置了页面的颜色。 4. 配置和使用 fullpage.js 可以在组件的 data 中设置 fullpage.js 的配置参数,例如页面滚动的速度、页面的动画效果、是否循环滚动等。可以在 options 对象中设置 fullpage.js 的配置参数,例如: ```javascript data() { return { options: { sectionsColor: ['#f2f2f2', '#4BBFC3', '#7BAABE'], scrollingSpeed: 1000, easingcss3: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)', loopBottom: true, loopTop: true, anchors: ['page1', 'page2', 'page3'] } } } ``` 可以使用 fullpage.js 提供的钩子函数,例如: ```javascript data() { return { options: { afterLoad: function(origin, destination, direction) { console.log('afterLoad', origin.index, destination.index, direction) }, onLeave: function(origin, destination, direction) { console.log('onLeave', origin.index, destination.index, direction) } } } } ``` 在这个示例中,我们定义了 `afterLoad` 和 `onLeave` 两个钩子函数,分别在页面滚动到新页面之后和离开当前页面之前执行。这些钩子函数可以用来执行一些特定的操作,例如修改页面标题、添加动画效果等。 这样就可以在 Vue.js 项目中使用 fullpage.js 了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值