我需要做一些类似但更灵活的事情。在不移动当前页面内容的情况下启用上述部分非常重要。
我刚开始使用FullPage.js,所以我没有尝试其他插件功能的任何问题。但我在这里分享结果。
这有点复杂,但它可以满足我的需求!例子在最后...
我不得不修改FullPage.js插件的两行:
function moveSectionUp(){
var prev = $(SECTION_ACTIVE_SEL).prevAll(SECTION_SEL + ':first'); // <--- THIS
// var prev = $(SECTION_ACTIVE_SEL).prev(SECTION_SEL); // <--- INSTEAD OF THIS
和
function moveSectionDown(){
var next = $(SECTION_ACTIVE_SEL).nextAll(SECTION_SEL + ':first'); // <--- THIS
//var next = $(SECTION_ACTIVE_SEL).next(SECTION_SEL); // <--- INSTEAD OF THIS
这些是添加的功能:
fpInitSkipEl = function(funcSkipEl) {
if ($.isFunction(funcSkipEl)) {
var nextIndex = 0;
$('.section').each(function() {
nextIndex++;
$('a[href="#' + $(this).attr('data-anchor') + '"]').on('click', function() {
var dataAnchor = $(this).attr('href').toString().replace('#', '');
return funcSkipEl($('.section').index($('.section.active')) + 1, $('.section').index($('.section[data-anchor="' + dataAnchor + '"]')) + 1);
});
});
}
}
fpSkipEl = function(anchorsToSkip, index, nextIndex) {
//debugger;
$('.section').each(function() {
if (anchorsToSkip.indexOf($(this).attr('data-anchor')) > -1
&& (index==-1 || $(this).attr('data-anchor') != $('.section').eq(index - 1).attr('data-anchor'))
&& (nextIndex==-1 || $(this).attr('data-anchor') != $('.section').eq(nextIndex - 1).attr('data-anchor'))) {
$(this).css('display', 'none').removeClass('fp-section');
} else {
$(this).css('display', '').addClass('fp-section');
}
$.fn.fullpage.reBuild();
});
}
fpGetRealIndex = function(index) {
var realIndex = 0;
$('.section').each(function() {
realIndex++;
if ($(this).hasClass('fp-section')) index--;
if (index == 0) return false;
});
return realIndex;
}
主要用途是这样的:
fpInitSkipEl(function(index, nextIndex) {
// Fire on anchor Click
// You can play with index (the current section) and nextIndex (the next section)
if (index==1 && nextIndex==4) {
fpSkipEl(['page2', 'page3'], index, nextIndex);
}
});
然后在afterLoad上初始化并设置你的逻辑
$('#fullpage').fullpage({
anchors: ['page1', 'page2', 'page3', 'page4'],
afterLoad: function(anchorLink, index) {
// Get the real index with the hidden sections, oterwise index is relative to the visible sections.
var realIndex = fpGetRealIndex(index);
fpSkipEl([], -1, -1); // Show all sections
}
});
The simple working example on JSFiddle
A more complex example on JSFiddle