jquery - fullpage.js 多个 onLeave 和 afterLoad 事件

标签 jquery html css fullpage.js

我正在尝试在我的 fullpage.js 中为不同的部分实现 afterLoad 和 onLeave 多个事件。但是只有最后一个似乎在工作,这里有什么问题吗?

如果我删除最后一个,第一个效果很好。所以看起来我以错误的方式组合了它们,所以一个禁用了另一个。

$('#fullpage').fullpage({
      onLeave: function(index, nextIndex, direction) {
          if (nextIndex == 3 || nextIndex == 1) {
              $('.aboutme').hide("slide", {
                  direction: "left"
              }, 200);
          }


      },
      afterLoad: function(anchorLink, index) {
          if (index == 2) {
              $('.aboutme').show("slide", {
                  direction: "left"
              }, 200);
          }

      },

      onLeave: function(index, nextIndex, direction) {

          if (nextIndex == 2) {
              $('.titlea').hide("slide", {
                  direction: "right"
              }, 200, function() {
                  $('.titleb').hide("slide", {
                      direction: "right"
                  }, 200);
              });
          }

      },
      afterLoad: function(anchorLink, index) {

          if (index == 1) {
              $('.titlea').show("slide", {
                  direction: "right"
              }, 200, function() {
                  $('.titleb').show("slide", {
                      direction: "right"
                  }, 200);
              });
          }
      }
  });

更具体地说,组合多个 onLeaveafterLoad 方法的正确方法是什么?

最佳答案

既然你的事件函数中已经有 if 子句,试试这个

$('#fullpage').fullpage({
        onLeave: function(index, nextIndex, direction){                
            if (nextIndex == 3 || nextIndex == 1 ) {
                $('.aboutme').hide("slide", { direction: "left" }, 200);
            } else if (nextIndex == 2 ) {
                $('.titlea').hide("slide", { direction: "right" }, 700, function(){$('.titleb').hide("slide", { direction: "right" }, 200);});
            }
        },
        afterLoad: function(anchorLink, index){
            if (index == 2){
                $('.aboutme').show("slide", { direction: "left" }, 200);
            } else if (index == 1 ) {
                $('.titlea').show("slide", { direction: "right" }, 700, function(){$('.titleb').show("slide", { direction: "right" }, 200);});
            }
        }   

    });   

});

或者使用 switch/case

$('#fullpage').fullpage({
        onLeave: function(index, nextIndex, direction){  
            switch(nextIndex){
                case 3:
                case 1:
                    $('.aboutme').hide("slide", { direction: "left" }, 200);
                    break;
                case 2:
                    $('.titlea').hide("slide", { direction: "right" }, 700, function(){$('.titleb').hide("slide", { direction: "right" }, 200);});
                    break;
            }
        },
        afterLoad: function(anchorLink, index){
            switch(index){
                case 2:
                    $('.aboutme').show("slide", { direction: "left" }, 200);
                    break;
                case 1:
                    $('.titlea').show("slide", { direction: "right" }, 700, function(){$('.titleb').show("slide", { direction: "right" }, 200);});
                    break;
            }
        }   

    });   

});

基本上,您有一个事件回调函数,并根据传递的参数决定要采取的操作。

关于jquery - fullpage.js 多个 onLeave 和 afterLoad 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33863628/

相关文章:

css - Magento 没有在第一次加载时加载 css

javascript - 重构正则表达式以不包含空组

javascript - 使用 animate css 按顺序动画元素

javascript - 如何使 jQuery ui datepicker 按钮图像只读

html - PowerShell 无序 HTML 列表

html - CSS 不适用于 IE 9

html - 隐藏特定的div onload,然后在点击后显示div

python - 抓取时摆脱python中的html标签

javascript - JVectorMap 设置比例低于 1

html - 如何在使用 Bootstrap 4 加载页面后禁用加载器/微调器?