粘性导航太迟了
Sticky Nav Sticks Too Late
我正在创建一个带有粘性导航的页面,它不会在标题图像移动后立即粘在顶部(它小于整个页面大小)。它只会在一个整页图像的大小过去之后才会出现。导航栏内的文字在粘贴后也会移动。
您可以在这里查看代码:https://jsfiddle.net/zinctan/7a436ojz/
这是我的javascript:
$(function() {
// when we scroll down the window, do this:
$(window).scroll(function(){
//Getting the scroll percentage
var windowHeight = $(window).height();
var scrollHeight = $(window).scrollTop();
var scrollPercentage = (scrollHeight / windowHeight);
console.log(scrollPercentage);
// if we have scrolled past 200, add the alternate class to nav bar
if(scrollPercentage > 1) {
$('.navHighlighter').addClass('scrolling');
} else {
$('.navHighlighter').removeClass('scrolling');
}
});
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^'//,'') == this.pathname.replace(/^'//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top - 80
}, 1000);
return false;
}
}
}); // code courtesy of CSS-Tricks
// apply the class of nav-active to the current nav link
$('a').on('click', function(e) {
e.preventDefault();
$('li.nav-active').removeClass('nav-active');
$(this).parent('li').addClass('nav-active');
});
// get an array of 'href' of each a tag
var navLink = $('ul.navHighlighter a');
console.log(navLink);
var aArray = [];
for(var i = 0; i < navLink.length; i++) {
console.log(i);
var aChild = navLink[i];
var navArray = $(aChild).attr('href');
console.log(navArray);
aArray.push(navArray);
console.log(aArray);
var selector = aArray.join(" , ");
console.log(selector);
}
$(window).scroll(function(){
var scrollTop = $(window).scrollTop();
var tops = [];
$(selector).each(function(){
var top = $(this).position().top - 90;
if(scrollTop > top) {
var id = $(this).attr('id');
$('.nav-active').removeClass('nav-active');
$('a[href="#'+id+'"]').parent().addClass('nav-active');
}
tops.push(top);
});
});
});
任何帮助都是有用的!谢谢:)
首先使用:
$(document).ready(function(){
});
,然后在该函数中编写jQuery代码,以确保您的脚本代码将在html网页完全加载后运行。
现在,我认为这应该是可行的:
$(document).ready(function() {
var topDist = $(".navHighlighter").position(); //save the position of your navbar, better use an id for that
$(document).scroll(function () {
var scroll = $(this).scrollTop();
if (scroll > topDist.top) { //when the scrolling reaches the very top of your navbar
$('.navHighlighter').addClass('scrolling');
} else {
$('.navHighlighter').removeClass('scrolling');
}
});
*rest of your code goes here*
});
同时,添加:
top:0;
width: 100%;
到你的。scrolling类,以便命令你的导航栏在用户窗口的顶部开始,并覆盖整个网页的宽度(position:fixed会产生一些问题,所以你必须设置元素的宽度,记住这一点)。
我希望我帮了忙,我满足了你的要求。编码快乐!:)
相关文章:
- 我的职位回报太快了,如何做出承诺
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- angular 1.5应用程序中的导航栏
- 无法从jquery Mobile导航栏重定向到另一个页面
- 引导程序:在导航栏中,显示悬停在单个位置的基于Li Link的不同内容
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- 导航到特定事件的另一个变量页面
- Wacom stu-430签名捕获速度太慢
- MVC 3页面导航和使用javascript传递参数
- 如何在react js中从一个页面导航到另一个页面
- 导航栏没有调整到浏览器屏幕的大小
- 显示放大镜弹出窗口时隐藏导航内容
- DataTable按下键选择扩展/导航
- 正在检测导航到<a name=“;最新主题”></a>
- 带有url的单页网站导航
- 停止图像以在导航中移动太远
- Jquery 移动导航栏填充错误:太窄
- 角度,数组复制太迟
- 粘性导航太迟了
- 等待异步电话结束-太迟了