[英]fullpage.js and adding/removing class at top of page
I'm having trouble with fullpage.js ( https://github.com/alvarotrigo/fullPage.js/ ) at http://vatuvara.com/ 我在http://vatuvara.com/上遇到了fullpage.js( https://github.com/alvarotrigo/fullPage.js/ )的问题
Basically I want #masthead to have a class of 'black-nav' added if the visitor is not in the first section of the page, and then I want this class removed if they are in the first section of the page. 基本上,如果访问者不在页面的第一部分,我希望#masthead添加一个“ black-nav”类,然后,如果访问者不在页面的第一部分,则我希望删除此类。
$(document).ready(function() {
$('#primary').fullpage({
menu: '#masthead',
afterLoad: function(anchor, index){
if(index == 1){
$('#masthead').removeClass('black-nav');
}else{
$('#masthead').addClass('black-nav');
}
}
});
});
This seems to work fine, except when I add a link to a section on the homepage eg. 这似乎工作正常,除非我在首页上添加了指向某个部分的链接。 the 'About' nav links to http://vatuvara.com/#TheIslands .
“关于”导航链接到http://vatuvara.com/#TheIslands 。 So I added a secondary script to change the masthead's class when #primary-menu a is clicked.
因此,当单击#primary-mena a时,我添加了一个辅助脚本来更改标头的类。
$(document).ready(function() {
$("#primary-menu a").click(function() {
$('#masthead').addClass('black-nav');
});
});
But the results are a bit mixed. 但是结果有点复杂。 Clicking on 'About', then scrolling back to the top of the page works about 50% of the time — the 'black-nav' class is removed, but the rest of the time it doesn't so #masthead continues to have the .black-nav class.
点击“关于”,然后滚动回到页面顶部,大约有50%的时间有效-“ black-nav”类已删除,但其余时间却没有,因此#masthead仍然拥有.black-nav类。
I also have another script which has similar results. 我也有另一个脚本,其结果类似。 I want the nav to be hidden when scrolling down, but re-appear when scrolling up.
我希望在向下滚动时隐藏导航,但在向上滚动时重新显示。 So I have this script below which seems to work about 70% of the time, the rest of the time #masthead continues to have the .black-nav class.
因此,在下面的脚本中,该脚本似乎可以正常运行约70%,其余时间#masthead仍然具有.black-nav类。 And if you scroll to the very bottom of the page, then scroll back up the success rate drops to about 50%
而且,如果您滚动到页面的最底端,然后向上滚动,则成功率将下降到大约50%
// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('#masthead').outerHeight();
$(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
// Make sure they scroll more than delta
if(Math.abs(lastScrollTop - st) <= delta)
return;
// If they scrolled down and are past the navbar, add class .nav-up.
// This is necessary so you never see what is "behind" the navbar.
if (st > lastScrollTop && st > navbarHeight){
// Scroll Down
$('#masthead').fadeOut();
} else {
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$('#masthead').fadeIn();
}
}
lastScrollTop = st;
}
I solved this by removing fullpage's afterLoad function and instead using a .scrollTop function: 我通过删除整页的afterLoad函数而不是使用.scrollTop函数来解决此问题:
$(document).ready(function() {
$('#primary').fullpage({
anchors: ['home', 'TheIslands', 'TheResort', 'Accomodation', 'AccomodationPhoto', 'Nature', 'NaturePhoto', 'CulinaryExperience', 'CulinaryExperienceLocations', 'CulinaryExperiencePhoto', 'CulinaryExperienceSeafood', 'Spa', 'SpaAbout', 'Activities', 'ActivitiesPhoto', 'Culture', 'Travel', 'Footer'],
menu: '#masthead',
slidesNavigation: true,
slidesNavPosition: 'bottom',
controlArrows: false,
scrollBar: true,
responsiveWidth: 900,
});
});
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 500) {
$("#masthead").addClass("black-nav");
} else {
$("#masthead").removeClass("black-nav");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.